Sample code for 30+ languages & platforms
Xbase++

Create a multipart/alternative MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.NewMultipartAlternative method, which initializes the object as an empty multipart/alternative entity. It takes no arguments; the alternative representations are then appended.

Background: multipart/alternative holds several renderings of the same content and lets the client pick the best one it can display — the standard way to send an email as both plain text and HTML. Order matters: parts go from least to most preferred, so the plain-text version comes first and the HTML last, and a client shows the last one it understands.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oMime
LOCAL oTextPart
LOCAL oHtmlPart

nSuccess := 0

//  Demonstrates the Mime.NewMultipartAlternative method, which initializes the object as an empty
//  multipart/alternative entity, clearing any existing content.  It takes no arguments.

oMime := CreateObject("Chilkat.Mime")

nSuccess := oMime:NewMultipartAlternative()
IF (nSuccess == 0)
    ? oMime:LastErrorText
    oMime:destroy()
    RETURN
ENDIF

//  multipart/alternative holds two representations of the SAME content; the client displays the
//  last one it can render.  Add a plain-text part, then an HTML part.
oTextPart := CreateObject("Chilkat.Mime")
nSuccess := oTextPart:SetBodyFromPlainText("Hello (plain text version).")
IF (nSuccess == 0)
    ? oTextPart:LastErrorText
    oMime:destroy()
    oTextPart:destroy()
    RETURN
ENDIF

nSuccess := oMime:AppendPart(oTextPart)
IF (nSuccess == 0)
    ? oMime:LastErrorText
    oMime:destroy()
    oTextPart:destroy()
    RETURN
ENDIF

oHtmlPart := CreateObject("Chilkat.Mime")
nSuccess := oHtmlPart:SetBodyFromHtml("<html><body><b>Hello</b> (HTML version).</body></html>")
IF (nSuccess == 0)
    ? oHtmlPart:LastErrorText
    oMime:destroy()
    oTextPart:destroy()
    oHtmlPart:destroy()
    RETURN
ENDIF

nSuccess := oMime:AppendPart(oHtmlPart)
IF (nSuccess == 0)
    ? oMime:LastErrorText
    oMime:destroy()
    oTextPart:destroy()
    oHtmlPart:destroy()
    RETURN
ENDIF

? "Number of alternatives: " + Str(oMime:NumParts)

oMime:destroy()
oTextPart:destroy()
oHtmlPart:destroy()