Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.0.0+

Read email headers and display the body as text

See more Email Object Examples

Read the sender, recipients, subject, date, and an optional custom header from an EML file. Display the plain-text body when available, or convert HTML to text. Handle messages that contain neither body format.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Email
long i
string ls_ReferenceId
oleobject loo_Reader

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

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
li_Success = loo_Email.LoadEml("with-attachments.eml")
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Email
    return
end if

Write-Debug "From: " + loo_Email.From
Write-Debug "Subject: " + loo_Email.Subject
Write-Debug "Date: " + loo_Email.EmailDateStr
i = 0
do while (i < loo_Email.NumTo)
    Write-Debug "To: " + loo_Email.GetToName(i) + "  " + loo_Email.GetToAddr(i)
    i = i + 1
loop
ls_ReferenceId = loo_Email.GetHeaderField("X-Reference-ID")
if loo_Email.LastMethodSuccess = 1 then
    Write-Debug "Reference: " + ls_ReferenceId
end if

if loo_Email.HasPlainTextBody() = 1 then
    Write-Debug loo_Email.GetPlainTextBody()
else
    if loo_Email.HasHtmlBody() = 1 then
        loo_Reader = create oleobject
        li_rc = loo_Reader.ConnectToNewObject("Chilkat.HtmlToText")

        Write-Debug loo_Reader.ToText(loo_Email.GetHtmlBody())
    else
        Write-Debug "No text or HTML body."
    end if

end if



destroy loo_Email
destroy loo_Reader