<< Click to Display Table of Contents >> Navigation: Samples and Tutorials > Visual FoxPro > Using AddEmail in FoxPro projects |
AddEmail ActiveX can be used to send emails from Visual FoxPro applications. To use AddEmail in your FoxPro project you need to add a code that creates SmtpMail object and sends a message using SimpleSend, SimpleSendAttachment or Send. Please use code snapshots below to get started.
Snapshot 1: simple text email.
PRIVATE objSmtpMail, strError, numResultCode
objSmtpMail = CREATEOBJECT("AddEmail.SmtpMail")
objSmtpMail.SmtpServer = "mail.myserver.com"
objSmtpMail.SmtpUsername = "jsmith"
objSmtpMail.SmtpPassword = "mypassword"
strError = ""
numResultCode = objSmtpMail.SimpleSend("jsmith@myserver.com", "jane@someserver.com;james@someserver.com", "test", "Test message", @strError)
IF numResultCode = 0 THEN
MESSAGEBOX("Sent successfully!")
ELSE
MESSAGEBOX(strError)
ENDIF
Snapshot 2: text email with attachments.
PRIVATE objSmtpMail, strError, numResultCode
objSmtpMail = CREATEOBJECT("AddEmail.SmtpMail")
objSmtpMail.SmtpServer = "mail.myserver.com"
objSmtpMail.SmtpUsername = "jsmith"
objSmtpMail.SmtpPassword = "mypassword"
strError = ""
numResultCode = objSmtpMail.SimpleSendAttachment("jsmith@myserver.com", "jane@someserver.com;james@someserver.com", "test", "Test message", "c:\files\doc1.pdf;c:\files\doc2.pdf", @strError)
IF numResultCode = 0 THEN
MESSAGEBOX("Sent successfully!")
ELSE
MESSAGEBOX(strError)
ENDIF
Snapshot 3: HTML message with attachments.
PRIVATE objSmtpMail, objMailMessage, objMailAttachment, objMailAddress
PRIVATE strError, numResultCode
objSmtpMail = CREATEOBJECT("AddEmail.SmtpMail")
objSmtpMail.SmtpServer = "mail.myserver.com"
objSmtpMail.SmtpUsername = "jsmith"
objSmtpMail.SmtpPassword = "mypassword"
* Create message and setup subject and body
objMailMessage = CREATEOBJECT("AddEmail.MailMessage")
objMailMessage.MessageBodyFormat = 1 && HTML format
objMailMessage.MessageSubject = "test"
objMailMessage.MessageBody = "<html><body><b>Testing...</b></body></html>"
* Add first attachment
objMailAttachment = CREATEOBJECT("AddEmail.MailAttachment")
objMailAttachment.File = "c:\files\doc1.pdf"
objMailMessage.AddAttachment(objMailAttachment)
* Add second attachment
objMailAttachment = CREATEOBJECT("AddEmail.MailAttachment")
objMailAttachment.File = "c:\files\doc2.pdf"
objMailMessage.AddAttachment(objMailAttachment)
* Setup sender
objMailAddress = CREATEOBJECT("AddEmail.MailAddress")
objMailAddress.Name = "John Smith"
objMailAddress.Address = "jsmith@myserver.com"
objMailMessage.Sender = objMailAddress
* Setup first recipient
objMailAddress = CREATEOBJECT("AddEmail.MailAddress")
objMailAddress.Name = "Jane Smith"
objMailAddress.Address = "jane@someserver.com"
objMailMessage.AddRecipient(objMailAddress)
* Setup second recipient
objMailAddress = CREATEOBJECT("AddEmail.MailAddress")
objMailAddress.Name = "James Smith"
objMailAddress.Address = "james@someserver.com"
objMailMessage.AddRecipient(objMailAddress)
* Send prepared message
numResultCode = objSmtpMail.Send(objMailMessage, .t., @strError)
IF numResultCode = 0 THEN
MESSAGEBOX("Sent successfully!")
ELSE
MESSAGEBOX(strError)
ENDIF
Please refer to the Reference section of this manual for detailed description of AddEmail objects, methods and properties.