Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

DataFlex Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)

Java KeyStore (JKS)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(DataFlex) Send HTML Email with Attachments

Demonstrates how to send an HTML email with file attachments.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoMailman
    Variant vEmail
    Handle hoEmail
    String sContentIdStarfish
    Handle hoSbHtml
    Integer iNumReplacements
    Boolean iSuccess
    String sContent
    String sTemp1
    Boolean bTemp1

    //  This example requires the Chilkat Email API to have been previously unlocked.
    //  See Unlock Chilkat MailMan 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

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.