Sample code for 30+ languages & platforms
DataFlex

Multipart/Alternative Email

It is very common for email to contain both plain-text and HTML alternative bodies. When a mail client such as Outlook, Eudora, or Thunderbird receives such an email, it will display one of the alternatives. The default is usually to display the HTML alternative. (In addition, the latest versions of email clients default to blocking HTML images from displaying for anti-SPAM reasons.) Emails with both plain-text and HTML alternative bodies are structured (in MIME) as multipart/alternative. The Chilkat Email component handles the MIME formatting of an email for you, so you don't have to worry about the details of the MIME composition. This example shows how to create an email with both plain-text and HTML alternative bodies.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    Handle hoEmail2
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End

    // First, add the typical header fields...
    Set ComSubject Of hoEmail To "This email has alternative bodies"
    Get ComAddTo Of hoEmail "Chilkat Support" "support@chilkatsoft.com" To iSuccess
    Set ComFrom Of hoEmail To "Matt <matt@chilkatsoft.com>"

    // Setting the Body property makes a text/plain email:
    Set ComBody Of hoEmail To "This is the plain-text body"

    // At this point, our email is a simple plain/text email
    // with no alternatives:
    Get ComSaveEml Of hoEmail "plainText.eml" To iSuccess

    // Now add an HTML alternative:
    Get ComAddHtmlAlternativeBody Of hoEmail "<html><body>This is the <b>HTML body</b></body></html>" To iSuccess
    Get ComSaveEml Of hoEmail "plainTextAndHtml.eml" To iSuccess

    // To create an HTML-only email, do this:
    Get Create (RefClass(cComChilkatEmail)) To hoEmail2
    If (Not(IsComObjectCreated(hoEmail2))) Begin
        Send CreateComObject of hoEmail2
    End

    // First, add the typical header fields...
    Set ComSubject Of hoEmail2 To "This email has alternative bodies"
    Get ComAddTo Of hoEmail2 "Chilkat Support" "support@chilkatsoft.com" To iSuccess
    Set ComFrom Of hoEmail2 To "Matt <matt@chilkatsoft.com>"

    Send ComSetHtmlBody To hoEmail2 "<html><body>This is the <b>HTML body</b></body></html>"
    Get ComSaveEml Of hoEmail2 "htmlOnly.eml" To iSuccess

    // Add a plain-text alternative:
    Get ComAddPlainTextAlternativeBody Of hoEmail2 "This is the plain-text body" To iSuccess
    Get ComSaveEml Of hoEmail2 "plainTextAndHtml2.eml" To iSuccess

    // To replace the plain-text alternative, just set the Body property:
    Set ComBody Of hoEmail2 To "This is the new plain-text body"

    // To replace the HTML alternative, call SetHtmlBody:
    Send ComSetHtmlBody To hoEmail2 "<html><body>This is the new HTML body...</body></html>"

    Get ComSaveEml Of hoEmail2 "plainTextAndHtml2a.eml" To iSuccess

    // To determine if an email has a particular alternative body,
    // call HasPlainTextBody or HasHtmlBody:
    Get ComHasPlainTextBody Of hoEmail2 To bTemp1
    If (bTemp1 = True) Begin
        Showln "Has plain-text body!"
    End

    Get ComHasHtmlBody Of hoEmail2 To bTemp1
    If (bTemp1 = True) Begin
        Showln "Has HTML body!"
    End



End_Procedure