Sample code for 30+ languages & platforms
PowerBuilder

Get an Alternative Body by Content-Type

See more Email Object Examples

Demonstrates the Chilkat Email.GetAlternativeBodyByContentType method, which returns an alternative body selected by its Content-Type — such as text/plain, text/html, or text/xml — instead of by numeric index. This example retrieves both the plain-text and HTML alternatives by content type.

Background: Selecting by Content-Type is often more convenient than by index, because you usually know which representation you want (the HTML, say) but not its position in the alternative list. This mirrors how a mail client works — it looks up the best matching type to display — and avoids assumptions about ordering, which can vary between messages.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
oleobject loo_Email
string ls_Plain
string ls_HtmlBody

//  Demonstrates the GetAlternativeBodyByContentType method, which returns an alternative
//  body selected by its Content-Type (such as text/plain, text/html, or text/xml) rather
//  than by index.

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")
if li_rc < 0 then
    destroy loo_Email
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Create an email with plain-text and HTML alternatives.
loo_Email.SetTextBody("This is the plain-text alternative.","text/plain")
loo_Email.AddHtmlAlternativeBody("<html><body>This is the HTML alternative.</body></html>")

//  Get the plain-text alternative by its content type.
ls_Plain = loo_Email.GetAlternativeBodyByContentType("text/plain")
Write-Debug "text/plain body: " + ls_Plain

//  Get the HTML alternative by its content type.
ls_HtmlBody = loo_Email.GetAlternativeBodyByContentType("text/html")
Write-Debug "text/html body: " + ls_HtmlBody


destroy loo_Email