Sample code for 30+ languages & platforms
DataFlex

Remove the HTML Body from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveHtmlAlternative method, which removes the HTML body from the email if one exists. Other body representations, attachments, and related items remain unchanged. This example builds a message with both plain-text and HTML alternatives, then removes the HTML.

Background: A multipart/alternative message carries the same content as both HTML and plain text. Dropping the HTML alternative yields a leaner, text-only message — useful for reducing size, sidestepping HTML-rendering or spam concerns, or enforcing a plain-text policy. The plain-text alternative remains as the message body. Its counterpart is RemovePlainTextAlternative.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoEmail
    Boolean iSuccess
    Integer iTemp1
    Boolean bTemp1

    //  Demonstrates the RemoveHtmlAlternative method, which removes the HTML body from the email
    //  (if one exists).  Other body representations, attachments, and related items remain.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Remove HTML alternative"

    //  Create an email with both plain-text and HTML alternatives.
    Send ComSetTextBody To hoEmail "This is the plain-text version." "text/plain"
    Get ComAddHtmlAlternativeBody Of hoEmail "<html><body>This is the HTML version.</body></html>" To iSuccess
    Get ComNumAlternatives Of hoEmail To iTemp1
    Showln "NumAlternatives before = " iTemp1
    Get ComHasHtmlBody Of hoEmail To bTemp1
    Showln "HasHtmlBody before: " bTemp1

    //  Remove the HTML body, leaving the plain-text alternative.
    Send ComRemoveHtmlAlternative To hoEmail
    Get ComNumAlternatives Of hoEmail To iTemp1
    Showln "NumAlternatives after = " iTemp1
    Get ComHasHtmlBody Of hoEmail To bTemp1
    Showln "HasHtmlBody after: " bTemp1


End_Procedure