Class with SMTP gmail

Post Reply
User avatar
Alexandre Oliveira
Posts: 10
Joined: Mon Sep 29, 2008 1:06 pm
Location: Brasil

Class with SMTP gmail

Post by Alexandre Oliveira »

How do I authenticate using a Gmail account?

Grateful.
Alexandre Oliveira
FHW 8.11 + xHarbour
Carlos Mora
Posts: 988
Joined: Thu Nov 24, 2005 3:01 pm
Location: Madrid, España

Re: Class with SMTP gmail

Post by Carlos Mora »

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"
User avatar
anserkk
Posts: 1280
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Class with SMTP gmail

Post 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
User avatar
RAMESHBABU
Posts: 591
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Re: Class with SMTP gmail

Post 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
User avatar
anserkk
Posts: 1280
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Class with SMTP gmail

Post 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
Jack
Posts: 249
Joined: Wed Jul 11, 2007 11:06 am

Re: Class with SMTP gmail

Post by Jack »

How to pass parameters ?
How to attach files ?

Thanks .
User avatar
anserkk
Posts: 1280
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Class with SMTP gmail

Post 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
User avatar
RAMESHBABU
Posts: 591
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Re: Class with SMTP gmail

Post 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
User avatar
anserkk
Posts: 1280
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Class with SMTP gmail

Post 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
User avatar
RAMESHBABU
Posts: 591
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Re: Class with SMTP gmail

Post 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
User avatar
Alexandre Oliveira
Posts: 10
Joined: Mon Sep 29, 2008 1:06 pm
Location: Brasil

Re: Class with SMTP gmail

Post by Alexandre Oliveira »

Thank you.

Problem solved.

[]s

Alexandre
Alexandre Oliveira
FHW 8.11 + xHarbour
Jack
Posts: 249
Joined: Wed Jul 11, 2007 11:06 am

Re: Class with SMTP gmail

Post by Jack »

Is it also possible to read the Inbox of Gmail and save attached files ?
Thanks .
User avatar
anserkk
Posts: 1280
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Class with SMTP gmail

Post 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
Post Reply