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 $SAMPLECODE$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
* 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

$SEEALSO$ Send email with CDO 2000

Comments

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.

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

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

For simple HTML you can use FILETOSTR() function
1
2
3
4
	...
	.cHtmlBody = FILETOSTR(lcHtmlFileName)
	...


If HTML includes images, you'll have to use cHtmlBodyUrl property of the class.
1
2
3
4
	...
	.cHtmlBodyUrl = "file://" + FULLPATH(lcHtmlFileName)
	...

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.

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

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.

Wonderful, works like a charm!

Great post. Thank you.

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

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. 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 2000 either.

Thank You !

Hi,

I want to display name actual name instead of my email address
1
2
3
4
5
6
7
8
      	...
      	.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

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.

1
2
3
4
5
6
7
8
      	...
      	.cUserName = "bgx3088@gmail.com"
      	.cPassword = "******"

      	.cFrom = "My display name" + "&lt;" + .cUserName + "&gt;"             &amp;&amp; My display name&lt;bgx3088@gmail.com&gt;
      	...


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

Hi Ray,

Something like
1
2
3
4
5
6
7
8
SELECT MyTableWithPayrollData
SCAN
  * Code to send email goes here
    ...
ENDSCAN


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

<strong>1429# send #188 #0x00000213 The transport failed to connect to the server.</strong>

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

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

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

Ok thank you

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

Hi Hans ,

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

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

Thank you for this code, it help me a lot.

Hi Sergey

I have been using your the above code to send emails via Gmail for years without any issues.
Last week I upgraded from Outlook 2002 to Outlook 2010 and it appears as thought CDO has been uninstalled.
I get "Class Definition CDO.Configuration is not found".
I did search Universalthread and located the CDOSYS.DLL but REGSVR32.EXE would not register it.
I am using Windows 7 64 bit.
Any ideas
Best regards
Kevin

Thanks for all your contributions, Sergey. This has been working great, but I've just run into a problem with PDF attachments. My program creates a PDF from a report and then emails it via Gmail. The PDF appears to be ok, since I can open it on the source machine and they are small enough (5 KB-40KB). However, when the email attachment is opened, I get a message from Adobe Reader: "There was an error processing a page. A file I/O error has occurred."

The odd thing is, if I step through this code in the command window, it works. When I realized that, I tried putting delays at various places in the process, to make sure the PDF was really completely created and the attachment was really "attached," but it hasn't helped.

Has anyone else seen this problem? Any ideas for resolving it?

Hi Allen,

It's not clear where error occurs.


hi Allen : Same thing happened with me. I am also using windows 7 .
I have watched that attachment PDF file size is increasing by 2KB.
Hence the same message is coming "A file I/O error has occurred".
Any solution please.

Great !! was the solution for a old routine to send email !
Thanks

hi

excellent prg under vfp9 it works very well but under my app standalone when i execute this procedure in my logiciel.exe error : cdo.configuration not found

thanks

The code works great for blindly sending a Gmail from VFP, however can it be modified to open the default mail client loaded with the parameters recipient, subject, body and attachment

When I try to send email from Gmail I get the following error:
1 ERROR::1429 # send # 226 # 0x00000213 CDO.Message.1 The transport failed to connect to the server

I had the same issue today.
So I then logged into the mail account (Outlook.com)
It asked me to verify some details (and use that Captcha nonsense)

Then I tried emailing again and all was sweet


I am receiving error : 1429 # send # 189 # 0x00000211 The message could not be sent to the SMTP server. The transport error code was 0x80040217. The server response was not available.

A user has been using the CDO email code for a while without error. But since moving to a new server, they are getting the following error:

Error while emailing to blahblah@gmail.com. ERROR #1429 send # 182 # 0x0000020F

The email option performs a FILETOSTR in order to read a template html file off the harddrive, performs substitution on the template text and then displays the msg on screen BEFORE it is sent. When its sent, the text is set as the email html body. Again, this error only started after moving to a new server. Any ideas?

When I try to send mail with CDO2000
I am receiving ERROR: 1429 # send 530 # 0x00000213 CDO.Message.1 The transport failed to connect to the server
It works in our network. But in another network gives this error. Where should I look at?

I want to create Gmail emails and save them to Gmail's "Draft" box. My client wants to review out-going emails before sending them. Is this possible with CDO2000?

Thanks.

Thank you very much, Sergey!!
Using VFP9 and Windows 7 64-bit and got your routine to work.
I've been trying many other solutions for the past few hours with no success.

I'm Using Dreamhost Mail SMTP and here are my settings:

WITH loMail
.cServer = "mail.yourdreamhostname.com"
.nServerPort = 587
.lUseSSL = .N.

.nAuthenticate = 1 && cdoBasic
.cUserName = 'myname@yourdreamhostname.com'
.cPassword = "password4myname"

* If From address doesn't match any of the registered identities,
* Gmail will replace it with your default Gmail address
.cFrom = 'myname@yourdreamhostname.com'
.cTo = 'recipient_name@gmail.com'

Thanks again!

Dear Sir

CDO2000 code works fine for operating systems win xp,win 7
but it fails for windows server 2003
error message is as follows
ERROR: :1429 # send # 164 # 0x00000213 CDO.Message.1 The transport failed to connect to the server.


Hi Sergey - can you please show us how to obtain a delivery confirmation?

Many thanks for your kind assistance!

Hi John,

See https://support.microsoft.com/en-us/kb/302839 and https://www.berezniker.com/comment/284#comment-284

Gracias por la contribución a la comunidad de VFP.

I have created an application which uses your excellent work which successfully sends emails via GMail on my computer and a number of others. However, on one remote client, this fails with the message
ERROR:1429#send#1230x00004e05 The requested body part was not found in the message.
When I use his gmail credentials on my computer, all works well. When he uses my credentials on his computer, he gets the same failure message. Any suggestion as to where to look first on his computer? I'm happy the app works OK and that it is not the gmail account.

Jim

thank u sir , i am glad to thank u very much i will send mail through in vfp coding first time with your help ,with your coding i am happy, thank u ,i m thankfull to you all time.

security problem created by your mail account , u can select less security in gmail account and try again it will go sucess, thank u
ERROR:1429#send#1230x00004e05
will solved