Class with SMTP gmail

Class with SMTP gmail

Postby Alexandre Oliveira » Tue Jun 09, 2009 1:26 pm

How do I authenticate using a Gmail account?

Grateful.
Alexandre Oliveira
FHW 8.11 + xHarbour
User avatar
Alexandre Oliveira
 
Posts: 10
Joined: Mon Sep 29, 2008 1:06 pm
Location: Brasil

Re: Class with SMTP gmail

Postby Carlos Mora » Tue Jun 09, 2009 6:37 pm

Use port 465
Saludos
Carlos Mora
http://harbouradvisor.blogspot.com/
StackOverflow http://stackoverflow.com/users/549761/carlos-mora
“If you think education is expensive, try ignorance"
Carlos Mora
 
Posts: 989
Joined: Thu Nov 24, 2005 3:01 pm
Location: Madrid, España

Re: Class with SMTP gmail

Postby anserkk » Wed Jun 10, 2009 4:17 am

Dear MR.Alexandre,

Here is a Sample code to send Email using Gmail.

Code: Select all  Expand view
oMailer:oEmailCfg := CREATEOBJECT( "CDO.Configuration" )
WITH OBJECT oMailer:oEmailCfg:Fields
   :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ):Value := "smtp.gmail.com"
   :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ):Value := 465
   :Item( "http://schemas.microsoft.com/cdo/configuration/sendusing" ):Value := 2   // Remote SMTP = 2, local = 1
   :Item( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" ):Value := .T.
   :Item( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" ):Value := .T.
   :Item( "http://schemas.microsoft.com/cdo/configuration/sendusername" ):Value := oMailer:cSendMailId  // Gmail A/c ID
   :Item( "http://schemas.microsoft.com/cdo/configuration/sendpassword" ):Value := oMailer:cSendMailIdPass   // Password
   :Item( "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"):Value := 30
   :Update()
END WITH

oEmailMsg := CREATEOBJECT ( "CDO.Message" )
WITH OBJECT oEmailMsg
   :Configuration = oMailer:oEmailCfg
   :From = chr(34)+" "+oMailer:cFromName+" "+chr(34)+ "<"+oMailer:cFromID+">" // This will be displayed as From address
   :To = cToEmailId    // To Email ID
   :BCC = cBcc         // BCC Id
   :Subject = oMailer:cSubject
   :Send()
END WITH


Regards

Anser
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Class with SMTP gmail

Postby RAMESHBABU » Wed Jun 10, 2009 6:32 am

Hi Mr.Carlos and Mr.Anser

I am getting the following error!.

Code: Select all  Expand view

Error description: Error CDO.Message/3  DISP_E_MEMBERNOTFOUND: SEND
 


Am I supposed to have any specific lib to get this working ?

Regards

- Ramesh Babu P

FWH 8.09, xHB 1.1.0 (Simplex), BCC55
User avatar
RAMESHBABU
 
Posts: 624
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Re: Class with SMTP gmail

Postby anserkk » Wed Jun 10, 2009 7:04 am

Dear Mr.Ramesh,

Please use TRY CATCH, it seems that the object is not created properly. Please note that u need active internet connection while testing the email send application.

It is working fine here.

Regards

Anser
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Class with SMTP gmail

Postby Jack » Wed Jun 10, 2009 10:21 am

How to pass parameters ?
How to attach files ?

Thanks .
Jack
 
Posts: 288
Joined: Wed Jul 11, 2007 11:06 am

Re: Class with SMTP gmail

Postby anserkk » Wed Jun 10, 2009 12:00 pm

Dear Mr.Jack,
How to pass parameters ?

What do you mean by parameters ?

How to attach files ?


Code: Select all  Expand view
oEmailMsg := CREATEOBJECT ( "CDO.Message" )
WITH OBJECT oEmailMsg
   :Configuration = oEmailCfg
   :From = chr(34)+" "+cFromName+" "+chr(34)+ "<"+cFromID+">" // This will be displayed as From address
   :To = cToEmailId    // To Email ID
   :BCC = cBcc         // BCC Id
   :Subject = cSubject
   :AddAttachment(cAttachment)    //  Add attachment "c:\LCd Projector.txt"
   :Send()
END WITH
 


Regards
Anser
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Class with SMTP gmail

Postby RAMESHBABU » Thu Jun 11, 2009 2:53 am

Hello Anser,

I tried with TRY, CATCH but I am getting the error exactly at :Send() method.
I am already connected to Internet, while I was testing this and I checked the
creation of all Objects. They are created properly.

I dont't know what is going wrong ?

- Ramesh Babu P
User avatar
RAMESHBABU
 
Posts: 624
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Re: Class with SMTP gmail

Postby anserkk » Thu Jun 11, 2009 4:21 am

Dear Ramesh,

Here is a complete sample which is working for me here. You need to provide only your gmail id and password in the code. If this is not working then, I believe that your firewall must be blocking the port 465. You may also try to increase the timeout value from 30 to some higher values

For Eg.
Code: Select all  Expand view
:Item( "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"):Value := 60


Code: Select all  Expand view
#Include "FiveWin.Ch"
*-------------------------------------------------*
Function Main()
*-------------------------------------------------*
Local oEmailCfg,oEmailMsg,oError,cHtml

cHtml:='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'
cHtml+='<HTML><HEAD>'
cHtml+='<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>'
cHtml+='<META name=GENERATOR content="MSHTML 8.00.6001.18783">'
cHtml+='<STYLE></STYLE>'
cHtml+='</HEAD>'
cHtml+='<BODY bgColor=#ffffff>'
cHtml+='<DIV><FONT size=2 face=Arial>Hello How are you ?</FONT></DIV></BODY></HTML>'


MSgInfo("I am going to Send Message")
TRY
  oEmailCfg := CREATEOBJECT( "CDO.Configuration" )
  WITH OBJECT oEmailCfg:Fields
     :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ):Value := "smtp.gmail.com"
     :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ):Value := 465
     :Item( "http://schemas.microsoft.com/cdo/configuration/sendusing" ):Value := 2   // Remote SMTP = 2, local = 1
     :Item( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" ):Value := .T.
     :Item( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" ):Value := .T.
     :Item( "http://schemas.microsoft.com/cdo/configuration/sendusername" ):Value := "YourGmailId@gmail.com"
     :Item( "http://schemas.microsoft.com/cdo/configuration/sendpassword" ):Value := "YourGmailPassword" // Password
     :Item( "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"):Value := 30
     :Update()
  END WITH
CATCH oError
  MsgInfo( "Could not send message" + ";"  + ;
         "Error: " + TRANSFORM(oError:GenCode, NIL) + ";" + ;
         "SubC: " + TRANSFORM(oError:SubCode, NIL) + ";" + ;
         "OSCode: " + TRANSFORM(oError:OsCode, NIL) + ";" + ;
         "SubSystem: " + TRANSFORM(oError:SubSystem, NIL) + ";" + ;
         "Message: " + oError:Description )
END
oError:=NIL

TRY
    oEmailMsg := CREATEOBJECT ( "CDO.Message" )
    WITH OBJECT oEmailMsg
        :Configuration = oEmailCfg
        :From = chr(34)+"Anser Test "+chr(34)+ "<anserkk@gmail.com>" // This will be displayed in the From (The email id does not appear)
        :To = "anserkk@hotmail.com"    // <-----   Place your email address
        :Subject = "Test Message"
        :MDNRequested = .T.
        :HTMLBody = cHtml
    END WITH
    oEmailMsg:Send()
CATCH oError
    MsgInfo("Could not send message" + ";"  + CRLF+ ;
            "Error: " + TRANSFORM(oError:GenCode, NIL) + ";" + CRLF+;
            "SubC: "  + TRANSFORM(oError:SubCode, NIL) + ";" + CRLF+ ;
            "OSCode: "+ TRANSFORM(oError:OsCode, NIL) + ";" + CRLF +;
            "SubSystem: " + TRANSFORM(oError:SubSystem, NIL) + ";" +CRLF+ ;
            "Message: " + oError:Description )
END
MsgInfo("Reached the end of the code")
Return .T.
 

Regards
Anser
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Class with SMTP gmail

Postby RAMESHBABU » Thu Jun 11, 2009 1:23 pm

Hello Anser,

Thank you very much for your example and detailed suggestions.

Your recent example has worked. And I could send my test mail.

- Ramesh Babu P
User avatar
RAMESHBABU
 
Posts: 624
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Re: Class with SMTP gmail

Postby Alexandre Oliveira » Mon Jun 15, 2009 12:09 pm

Thank you.

Problem solved.

[]s

Alexandre
Alexandre Oliveira
FHW 8.11 + xHarbour
User avatar
Alexandre Oliveira
 
Posts: 10
Joined: Mon Sep 29, 2008 1:06 pm
Location: Brasil

Re: Class with SMTP gmail

Postby Jack » Tue Jun 23, 2009 2:53 pm

Is it also possible to read the Inbox of Gmail and save attached files ?
Thanks .
Jack
 
Posts: 288
Joined: Wed Jul 11, 2007 11:06 am

Re: Class with SMTP gmail

Postby anserkk » Wed Jun 24, 2009 12:25 pm

Hi Mr.Jack,

As per Microsoft, GetStream() returns the Stream object containing the complete message, including headers and all content, in serialized (wire-transport) format. I am not sure and I have not yet tried it. The information may be useful to you.

For Ref.
http://msdn.microsoft.com/en-us/library/ms526453(EXCHG.10).aspx

Code: Select all  Expand view
Local iMsg,iDSrc,iBp
// Get the IMessage interface (Dispatch)
iMsg = CreateObject("CDO.Message")
// Get the IDataSource on the Message Object (IDispatch)
iDSrc = iMsg:GetInterface("IDataSource")
//Get the IBodyPart interface on the Message Object (IDispatch)
iBp = iMsg:GetInterface("IBodyPart")


Regards
Anser
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 28 guests