Send email via Gmail account

Gmail SMTP server requires SSL connection on port 465.

Known Gmail limits on sending emails:

  • An attachment size is limited to 25 MB
  • An individual email can be sent to maximum of 100 recipients
  • A large number of undeliverable messages could trigger an account lockdown

Exceeding any limits beside the attachment size will cause Gmail account to be locked down for 24-72 hours.
 
Sample code below uses CDO 2000 class for sending emails

This is sample code. Add error handling and adjust to your requirements as necessary.

* Replace addresses with real ones before running the code
 
loMail = NEWOBJECT("Cdo2000", "Cdo2000.fxp")
 
WITH loMail
	.cServer = "smtp.gmail.com"
	.nServerPort = 465
	.lUseSSL = .T.
 
	.nAuthenticate = 1 	&& cdoBasic
	.cUserName = "yourGmailAccount@gmail.com"
	.cPassword = "yourGmailPassword"
 
	* If From address doesn't match any of the registered identities, 
	*	Gmail will replace it with your default Gmail address
	.cFrom = "yourGmailAccount@gmail.com"
 
	.cTo = "somebody@otherdomain.com, somebodyelse@otherdomain.com"
 
	.cSubject = "CDO 2000 email through Gmail SMTP server"
 
	* Uncomment next lines to send HTML body
	*.cHtmlBody = "<html><body><b>This is an HTML body<br>" + ;
	*		"It'll be displayed by most email clients</b></body></html>" 	
 
	.cTextBody = "This is a text body." + CHR(13) + CHR(10) + ;
			"It'll be displayed if HTML body is not present or by text only email clients"
 
	* Attachments are optional
	* .cAttachment = "myreport.pdf, myspreadsheet.xls"
ENDWITH
 
IF loMail.Send() > 0
	FOR i=1 TO loMail.GetErrorCount()
		? i, loMail.Geterror(i)
	ENDFOR
	* Clear errors
	loMail.ClearErrors()
ELSE
	? "Email sent."
ENDIF

See Also Send email with CDO 2000

Your rating: None Average: 4.6 (9 votes)

Thanks - this is very helpful

Thank you so much for posting this. I have been trying to send SMTP mail with my Gmail account from Visual FoxPro using several different techniques, none of which were working. This one worked like a charm.

Exactly what I need

Thank you very much!!!!
Exactly what I need!

How to send HTML file as email body

I have a HTML file and i want to send this file as HTML body.

Send HTML file as email body

For simple HTML you can use FILETOSTR() function

	...
	.cHtmlBody = FILETOSTR(lcHtmlFileName)
	...

If HTML includes images, you'll have to use cHtmlBodyUrl property of the class.

	...
	.cHtmlBodyUrl = "file://" + FULLPATH(lcHtmlFileName)
	...

HTML body

i have a mht html file and i want to send this file as body of message

lcHtmlFileName='report03-01-2010.mht'

.cHtmlBodyUrl = "file://" + FULLPATH(lcHtmlFileName)

giving me error as below

ERROR: :1429 # send # 103 # 0x00000216 CDO.Message.1 The content type was not valid in this context. For example, the root of an MHTML message must be an HTML document.

warm regards,
mk.

Re: HTML body

It sounds like your .MHT file is not a complete MHTML but a fragment. You can try to wrap it into <HTML> and <BODY> tags or add whatever tags are missing to make it complete MHTML.

Re: Re: HTML body

The file is created with XFRX. XFRX has an option to create a MHTML file.

How can I send the MHTML file created with XFRX?

warm regards,
mk.

Sending email with CDO 2000

Wonderful, works like a charm!

Great post. Thank you.

Great post. Thank you.

Can I save a message directly to Gmail folder?

Please, how can I save the message directly to the folder concept of Gmail by CDO2000 (without sending)? Thank you for your advice!

Re: Can I save a message directly to Gmail folder?

First, Gmail does not have folders. If you mean Labels, the answer is no. You can use CDO 2000 to send email to your Gmail account and set filters in Gmail to route messages appropriately.

Thank you for your answer.

Thank you for your answer. Before I send email, I need to open Gmail to check or complete message and then send. How can I do it?

You cannot do that with CDO

You cannot do that with CDO 2000 either.

Perfect

Thank You !

How to Display real name instead of email address

Hi,

I want to display name actual name instead of my email address

      	...
      	.cUserName = "bgx3088@gmail.com"
      	.cPassword = "******"
 
      	.cFrom = .cUserName
      	...

How to display my name "Yeo" in the from field instead of bgx3088@gmail.com.

Rgrds and Thanks
Yeo

Re:How to Display real name instead of email address

You have no control how From address will be displayed at destination. It will depends on email client used. But you can add a name part that could be displayed by an email client.

      	...
      	.cUserName = "bgx3088@gmail.com"
      	.cPassword = "******"
 
      	.cFrom = "My display name" + "<" + .cUserName + ">"             && My display name<bgx3088@gmail.com>
      	...

Send multiple messages

Thanks, works great.

But how can I put it inside a loop, so that I can send different messages to different people ?
e.g. in a payroll system, to send monthly pay slip to diferent people

Re: Send multiple messages

Hi Ray,

Something like

SELECT MyTableWithPayrollData
SCAN
  * Code to send email goes here
    ...
ENDSCAN

Error when sending email

We are currently trying to send an email in a win7 environment and constantly receiving the error

1429# send #188 #0x00000213 The transport failed to connect to the server.

We are using a google apps acount, on port 587 and ssl true.

Is there any specific reason as to why this is happening.

Re:Error when sending email

You're using incorrect port. Port 587 is for TLS/STARTTLS which CDO 2000 does not support.

Ok thank you

Ok thank you

Failed to connect to server

Hi Sergey,
I was very happy to find such an excellent solution of sending mail from VFP. However...
I also receive this error message, "not able to connect to server", when I was sending
to a GMX account and tried both 25 and 587 ports.
Some servers have a wait time of 30 seconds when they receive mail through port 25,
because they want to avoid spam mails. Do you know of any problems sending through
GMSX and using port 25?
Greetings from Germany
Hans

Re: Failed to connect to server

Hi Hans ,

Only port 465 can be used with CDO 2000 to access Gmail.

Sorry for my blindnes

Hi Sergey,
... false alarm...
I did not see, that you actually mentioned, that GMX requires port 465.
Sorry for bothering you. And THANK YOU for your nice work.
Cheers
Hans