Sample code for 30+ languages & platforms
DataFlex

Get Each Alternative Body of an Email

See more Email Object Examples

Demonstrates the Chilkat Email.GetAlternativeBody method, which returns the Nth alternative body. The NumAlternatives property gives the number of alternatives, and indexes are zero-based. This example builds a message with plain-text and HTML alternatives and prints each one.

Background: A multipart/alternative email carries the same content in several forms — commonly plain text and HTML. Enumerating them by index lets you pull out exactly the representation you need: showing the HTML in a rich viewer, extracting the plain text for search or indexing, or comparing the two. Pair this with GetAlternativeContentType to learn what each indexed body actually is.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoEmail
    Boolean iSuccess
    Integer n
    Integer i
    String sTemp1

    //  Demonstrates the GetAlternativeBody method, which returns the Nth alternative body.
    //  The NumAlternatives property gives the number of alternatives; indexes are zero-based.

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

    //  Create an email with plain-text and HTML alternatives.
    Send ComSetTextBody To hoEmail "This is the plain-text alternative." "text/plain"
    Get ComAddHtmlAlternativeBody Of hoEmail "<html><body>This is the HTML alternative.</body></html>" To iSuccess

    Get ComNumAlternatives Of hoEmail To n

    For i From 0 To (n - 1)
        Showln "---- Alternative " i " ----"
        Get ComGetAlternativeBody Of hoEmail i To sTemp1
        Showln sTemp1
    Loop



End_Procedure