DataFlex
DataFlex
Send HTML Email with Attachments
See more SMTP Examples
Demonstrates how to send an HTML email with file attachments.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoMailman
Variant vEmail
Handle hoEmail
String sContentIdStarfish
Handle hoSbHtml
Integer iNumReplacements
String sContent
String sTemp1
Boolean bTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatMailMan)) To hoMailman
If (Not(IsComObjectCreated(hoMailman))) Begin
Send CreateComObject of hoMailman
End
Set ComSmtpHost Of hoMailman To "smtp.my-starttls-mail-server.com"
Set ComSmtpUsername Of hoMailman To "MY-SMTP-USERNAME"
Set ComSmtpPassword Of hoMailman To "MY-SMTP-PASSWORD"
Set ComStartTLS Of hoMailman To True
Set ComSmtpPort Of hoMailman To 587
// Create a new email object
Get Create (RefClass(cComChilkatEmail)) To hoEmail
If (Not(IsComObjectCreated(hoEmail))) Begin
Send CreateComObject of hoEmail
End
Set ComSubject Of hoEmail To "Test SMTP API to Send HTML Email with Attachments"
Set ComFrom Of hoEmail To "Joe Programmer <joe@my-starttls-mail-server.com>"
Get ComAddTo Of hoEmail "Chilkat Support" "support@chilkatsoft.com" To iSuccess
// Add a plain-text alternative body, which will likely never be seen.
// (It is shown if the receiving email client is incapable of displaying HTML email.)
Get ComAddPlainTextAlternativeBody Of hoEmail "This is a plain-text alternative body..." To iSuccess
// Our HTML will include an image, so add the related image here.
Get ComAddRelatedFile Of hoEmail "qa_data/jpg/starfish.jpg" To sContentIdStarfish
Get ComLastMethodSuccess Of hoEmail To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoEmail To sTemp1
Showln sTemp1
Procedure_Return
End
// The src attribute for the image tag is set to the contentIdStarfish:
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbHtml
If (Not(IsComObjectCreated(hoSbHtml))) Begin
Send CreateComObject of hoSbHtml
End
Get ComAppend Of hoSbHtml "<html><body><p>This is an HTML email with an embedded image.</p>" To iSuccess
Get ComAppend Of hoSbHtml '<p><img src="cid:CONTENT_ID_STARFISH" /></p></body></html>' To iSuccess
Get ComReplace Of hoSbHtml "CONTENT_ID_STARFISH" sContentIdStarfish To iNumReplacements
Get ComGetAsString Of hoSbHtml To sTemp1
Get ComAddHtmlAlternativeBody Of hoEmail sTemp1 To iSuccess
// Finally, add some attachments to the email.
// Add a file attachment.
Get ComAddFileAttachment2 Of hoEmail "qa_data/pdf/fishing.pdf" "application/pdf" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoEmail To sTemp1
Showln sTemp1
Procedure_Return
End
// Add an attachment where the content is contained in a string.
Move "This is the content of the 2nd attached file." To sContent
Get ComAddStringAttachment Of hoEmail "someText.txt" sContent To iSuccess
// Send the HTML email.
Get pvComObject of hoEmail to vEmail
Get ComSendEmail Of hoMailman vEmail To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoMailman To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComCloseSmtpConnection Of hoMailman To iSuccess
If (iSuccess <> True) Begin
Showln "Connection to SMTP server not closed cleanly."
End
Showln "HTML email with attachments sent!"
End_Procedure