Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
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)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(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-win32.pkg

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

    // 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

 

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