Sample code for 30+ languages & platforms
DataFlex

Send HTML Email with CSS as Related Item using Content-Location

See more SMTP Examples

Demonstrates how to compose an HTML email with an external CSS file included as a related item and referenced by Content-Location.

Some email clients display embedded (related) content best using CID's (Content-IDs), whereas other email clients display related content best by Content-Location. The choice you make may depend on the software used by the intended recipient of your email. (Does' the recipient read email on an iPhone? Android? Outlook? GMail? Thunderbird? etc.)

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMailman
    Variant vEmail
    Handle hoEmail
    Handle hoSbCss
    Boolean iBCrlf
    String sFilenameInHtml
    Handle hoSbHtml
    String sTemp1

    Move False To iSuccess

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // The mailman object is used for sending and receiving email.
    Get Create (RefClass(cComChilkatMailMan)) To hoMailman
    If (Not(IsComObjectCreated(hoMailman))) Begin
        Send CreateComObject of hoMailman
    End

    // Use your SMTP server hostname.  This example uses office365, but it could be any SMTP server..
    Set ComSmtpHost Of hoMailman To "outlook.office365.com"
    Set ComSmtpPort Of hoMailman To 587
    Set ComStartTLS Of hoMailman To True

    // Set the SMTP login/password
    Set ComSmtpUsername Of hoMailman To "OFFICE365-SMTP-LOGIN"
    Set ComSmtpPassword Of hoMailman To "OFFICE365-SMTP-PASSWORD"

    // 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 "HTML Email with embedded CSS"
    Set ComFrom Of hoEmail To "Chilkat Support <my-office365-user@mydomain.com>"
    Get ComAddTo Of hoEmail "Chilkat Support" "support@chilkatsoft.com" To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbCss
    If (Not(IsComObjectCreated(hoSbCss))) Begin
        Send CreateComObject of hoSbCss
    End
    Move True To iBCrlf
    Get ComAppendLine Of hoSbCss "body {" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbCss "    background-color: powderblue;" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbCss "}" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbCss "h1 {" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbCss "    color: blue;" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbCss "}" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbCss "p {" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbCss "    color: red;" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbCss "}" iBCrlf To iSuccess

    // The filenameInHtml is what should exists within the HTML (in the href atribute)
    Move "styles.css" To sFilenameInHtml
    // Call AddRelatedString2 to use Content-Location.
    Get ComGetAsString Of hoSbCss To sTemp1
    Send ComAddRelatedString2 To hoEmail sFilenameInHtml sTemp1 "utf-8"

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbHtml
    If (Not(IsComObjectCreated(hoSbHtml))) Begin
        Send CreateComObject of hoSbHtml
    End
    Get ComAppendLine Of hoSbHtml "<!DOCTYPE html>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbHtml "<html>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbHtml "<head>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbHtml '  <link rel="stylesheet" href="styles.css">' iBCrlf To iSuccess
    Get ComAppendLine Of hoSbHtml "</head>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbHtml "<body>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbHtml "" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbHtml "<h1>This is a heading</h1>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbHtml "<p>This is a paragraph.</p>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbHtml "" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbHtml "</body>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbHtml "</html>" iBCrlf To iSuccess

    Get ComGetAsString Of hoSbHtml To sTemp1
    Send ComSetHtmlBody To hoEmail sTemp1

    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
    End
    Else Begin
        Showln "Mail Sent!"
    End



End_Procedure