Send email with CDO 2000
It's simple and convenient to send unattended email with CDO 2000 as long as there's a SMTP server available.
Note 1 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.somedomain.com" * Uncomment following lines and fill your credentials * if your SMTP server requires basic authentication *.nAuthenticate = 1 && cdoBasic *.cUserName = "myusername" *.cPassword = "mypassword" .cFrom = "somemailbox@somedomain.com" .cTo = "somebody@otherdomain.com, somebodyelse@otherdomain.com" .cSubject = "CDO 2000 email" * 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
CDO2000 cHTMLBodyURL file access denied after 1st Use
Hi, Sergey,
Thanks so much for the example CDO200 stuff you have posted.
I have one problem. Using the cHtmlBodyURL property, I can successfully place the contents of an HTML file I created in Visual Foxpro code into the body of the sent message. However, this only works the first time through (in a Visual Fox session). If I try to run it again, I get "file access denied" when trying to re-create my HTML file by using SET ALTERNATE TO. It seems CDO2000 is keeping a lock on the file. If I quit Fox, and come back in and run the program, it works fine.
I have tried all sorts of RELEASE and CLOSE commands in the program, but nothing works.
Any ideas on what I'm missing?
Thanks,
Bill
Re: CDO2000 cHTMLBodyURL file access denied after 1st Use
Bill,
Did you try to release CDO object?
CDO2000 File Access Problem Solved!
Sergey,
In another gem you wrote about using Gmail and CDO2000, I found TWO solutions to the problem.
In my program, I set "savefile" to a file name, then used SET ALTERNATE to write the HTML code to it (and closing it properly!). Then, either one of the following works, AND allows the creation of the file and SEND to be performed consecutive times without the dreaded "File Access denied".
1. .cHtmlBody = FILETOSTR(savefile) && this works! No mysterious file lock on savefile afterwards
2. .cHtmlBodyUrl = "file://" + FULLPATH(savefile) && this also works, no mysterious file lock on savefile afterwards
For some reason, using just the file name (savefile) in the cHTMLBodyURL causes the weird lock on savefile afterwards.
Thanks for all your help, Sergey.
Problem with Chinese characters
Hi Sergey,
Thanks for your code. It works well for English contents but Chinese characters will become garbage. Is there any way to fix the problem?
Thanks,
Ben Tam
Re: Problem with Chinese characters
Hi Ben,
Maybe How To Send Japanese Characters by Using CDO for Windows 2000 can help?
Changes to use additional authentication
What changes do I need if they require additional authentication and it is different than their normal login and password?
Thanks.
Don
Re: Changes to use additional authentication
Hi Don,
The CDO 2000 supports only following three types of authentication:
Sending a Batch of eMails with one call to Send()
Hi Sergey
Is it possible to create a number of email messages to different people with different attachments etc. and then just call the Send() method once to send all the messages?
Thanks,
Simon
Re: Sending a Batch of eMails with one call to Send()
Hi Simon,
You'll have to call Send() once per each email. There's no batch mode.