Sample code for 30+ languages & platforms
DataFlex

Get the Content-Type of Each Alternative Body

See more Email Object Examples

Demonstrates the Chilkat Email.GetAlternativeContentType method, which returns the content type of 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 the content type of each.

Background: Each alternative in a multipart/alternative message declares a Content-Type such as text/plain or text/html. Enumerating those types tells you which representations a message actually contains before you fetch a body — letting you decide, for instance, whether an HTML version exists or whether you must fall back to plain text. It is the natural companion to GetAlternativeBody, which fetches the body at the same index.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

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

    //  Demonstrates the GetAlternativeContentType method, which returns the content type of
    //  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)
        Get ComGetAlternativeContentType Of hoEmail i To sTemp1
        Showln "Alternative " i " content type: " sTemp1
    Loop



End_Procedure