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
ELSE
	? "Email sent."
ENDIF

See Also Send email with CDO 2000

Your rating: None Average: 3.7 (3 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.