Page 1 of 1

Class with SMTP gmail

Posted: Tue Jun 09, 2009 1:26 pm
by Alexandre Oliveira
How do I authenticate using a Gmail account?

Grateful.

Re: Class with SMTP gmail

Posted: Tue Jun 09, 2009 6:37 pm
by Carlos Mora
Use port 465

Re: Class with SMTP gmail

Posted: Wed Jun 10, 2009 4:17 am
by anserkk
Dear MR.Alexandre,

Here is a Sample code to send Email using Gmail.

Code: Select all

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

Re: Class with SMTP gmail

Posted: Wed Jun 10, 2009 6:32 am
by RAMESHBABU
Hi Mr.Carlos and Mr.Anser

I am getting the following error!.

Code: Select all

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

Re: Class with SMTP gmail

Posted: Wed Jun 10, 2009 7:04 am
by anserkk
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

Re: Class with SMTP gmail

Posted: Wed Jun 10, 2009 10:21 am
by Jack
How to pass parameters ?
How to attach files ?

Thanks .

Re: Class with SMTP gmail

Posted: Wed Jun 10, 2009 12:00 pm
by anserkk
Dear Mr.Jack,
How to pass parameters ?
What do you mean by parameters ?
How to attach files ?

Code: Select all

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

Re: Class with SMTP gmail

Posted: Thu Jun 11, 2009 2:53 am
by RAMESHBABU
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

Re: Class with SMTP gmail

Posted: Thu Jun 11, 2009 4:21 am
by anserkk
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

:Item( "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"):Value := 60

Code: Select all

#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

Re: Class with SMTP gmail

Posted: Thu Jun 11, 2009 1:23 pm
by RAMESHBABU
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

Re: Class with SMTP gmail

Posted: Mon Jun 15, 2009 12:09 pm
by Alexandre Oliveira
Thank you.

Problem solved.

[]s

Alexandre

Re: Class with SMTP gmail

Posted: Tue Jun 23, 2009 2:53 pm
by Jack
Is it also possible to read the Inbox of Gmail and save attached files ?
Thanks .

Re: Class with SMTP gmail

Posted: Wed Jun 24, 2009 12:25 pm
by anserkk
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 ... G.10).aspx

Code: Select all

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