Created
September 26, 2017 10:53
-
-
Save anonymous/ddb479e315bcc45efb0d4703fc05b836 to your computer and use it in GitHub Desktop.
Send email from Visual FoxPro using a GMail account and CDO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
** -- I based this on code from Sweet Potato Software (https://www.sweetpotatosoftware.com/) | |
******************************* | |
*!* Example of using SendViaCDOSYS | |
******************************* | |
*DIMENSION aryAttach(2) | |
*aryAttach(1) = "C:\attachment1.txt" && change to an actual file that exists on your computer | |
*aryAttach(2) = "C:\attachment2.zip" && change to an actual file that exists on your computer | |
aryAttach=.F. | |
Local lcFrom, lcTo, lcSubject, lcBody, lcCC, lcBCC, lcMailServer, lcUserName, lcPassword, llHTMLFormat, lcErrReturn | |
lcFrom = "your.account@gmail.com" | |
lcTo = "some.recipient@somewhere.com" | |
lcSubject = "Hey Have You Tried VFP Email?" | |
*!* Sending the body in HTML format | |
llHTMLFormat = .T. && change to .F. to send plain text message | |
lcBody = "<a href='http://www.sweetpotatosoftware.com/SPSBlog/default.aspx'>" + ; | |
"Hey Have You Tried VFP Email?" + ; | |
"</a>" | |
lcCC = "" | |
lcBCC = "" | |
lcMailServer = "smtp.gmail.com" && my SMTP Server | |
lcUserName = "your.account@gmail.com" && my SMTP username | |
lcPassword = "mypassword" && my SMTP password | |
SendViaCDOSYS(@lcErrReturn, lcFrom, lcTo, lcSubject, lcBody, @aryAttach, lcCC, lcBCC, lcMailServer, lcUserName, lcPassword, llHTMLFormat) | |
If Empty(lcErrReturn) | |
Messagebox("'" + lcSubject + "' sent successfullly.", 64, "Send email via CDOSYS") | |
Else | |
Messagebox("'" + lcSubject + "' failed to be sent. Reason:" + Chr(13) + lcErrReturn, 64, "Send email via CDOSYS") | |
Endif | |
******************************************* | |
Procedure SendViaCDOSYS(tcReturn, tcFrom, tcTo, tcSubject, tcBody, taFiles, tcCC, tcBCC, tcMailServer, tcUserName, tcPassword, tlHTMLFormat) | |
******************************************* | |
Local lcSchema, loConfig, loMsg, loAtt, lnCountAttachments | |
Try | |
lcSchema = "http://schemas.microsoft.com/cdo/configuration/" | |
loConfig = Createobject("CDO.Configuration") | |
With loConfig.Fields | |
.Item(lcSchema + "smtpserverport") = 465 && SMTP Port gmail 465, 587 | |
.Item(lcSchema + "sendusing") = 2 && Send it using port | |
.Item(lcSchema + "smtpserver") = tcMailServer && host of smtp server | |
.Item(lcSchema + "smtpauthenticate") = 1 && Authenticate | |
.Item(lcSchema + "sendusername") = tcUserName && Username | |
.Item(lcSchema + "sendpassword") = tcPassword && Password | |
.Item(lcSchema + "smtpusessl") = .t. | |
.Update | |
Endwith | |
loMsg = Createobject ("CDO.Message") | |
loMsg.Configuration = loConfig | |
With loMsg | |
.From = tcFrom | |
.To = tcTo | |
If Type("tcCC") = "C" | |
.CC = tcCC | |
Endif | |
If Type("tcBCC") = "C" | |
.BCC = tcBCC | |
Endif | |
.Subject = tcSubject | |
If tlHTMLFormat | |
.HTMLBody = tcBody | |
Else | |
.TextBody = tcBody | |
Endif | |
If Type("tafiles",1) = "A" | |
For lnCountAttachments = 1 To Alen(taFiles) | |
loAtt=.AddAttachment(taFiles(lnCountAttachments)) | |
Endfor | |
Endif | |
.Send() | |
Endwith | |
Catch To loError | |
tcReturn = [Error: ] + Str(loError.ErrorNo) + Chr(13) + ; | |
[LineNo: ] + Str(loError.Lineno) + Chr(13) + ; | |
[Message: ] + loError.Message + Chr(13) + ; | |
[Procedure: ] + loError.Procedure + Chr(13) + ; | |
[Details: ] + loError.Details + Chr(13) + ; | |
[StackLevel: ] + Str(loError.StackLevel) + Chr(13) + ; | |
[LineContents: ] + loError.LineContents | |
Finally | |
Release loConfig, loMsg | |
Store .Null. To loConfig, loMsg | |
Endtry | |
Endproc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment