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

Reply to an email and preserve its thread

See more Email Object Examples

Create a reply to with-attachments.eml. Address it to Reply-To when supplied, or to the original author otherwise. Link the reply to the original Message-ID, replace its body with a short answer, and print the destination before saving the draft.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Original
oleobject loo_Reply
string ls_Destination
string ls_ParentId
oleobject loo_Refs

li_Success = 0

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

loo_Original = create oleobject
li_rc = loo_Original.ConnectToNewObject("Chilkat.Email")
if li_rc < 0 then
    destroy loo_Original
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Original.LoadEml("with-attachments.eml")
if li_Success <> 1 then
    Write-Debug loo_Original.LastErrorText
    destroy loo_Original
    return
end if

loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.Email")

li_Success = loo_Original.ToReply(loo_Reply)
if li_Success <> 1 then
    Write-Debug loo_Original.LastErrorText
    destroy loo_Original
    destroy loo_Reply
    return
end if

loo_Reply.From = "Alex Rivera <alex@example.net>"
//  This is a reply to the author, not a reply-all. Set the destination explicitly.
loo_Reply.ClearTo()
loo_Reply.ClearCC()
loo_Reply.ClearBcc()
ls_Destination = loo_Original.GetHeaderField("Reply-To")
if ls_Destination = "" then
    ls_Destination = loo_Original.From
end if

li_Success = loo_Reply.AddMultipleTo(ls_Destination)
if li_Success <> 1 then
    Write-Debug loo_Reply.LastErrorText
    destroy loo_Original
    destroy loo_Reply
    return
end if

//  Link this answer to its parent and preserve the earlier conversation references.
ls_ParentId = loo_Original.GetHeaderField("Message-ID")
if ls_ParentId <> "" then
    loo_Reply.AddHeaderField("In-Reply-To",ls_ParentId)
    loo_Refs = create oleobject
    li_rc = loo_Refs.ConnectToNewObject("Chilkat.StringBuilder")

    loo_Refs.Append(loo_Original.GetHeaderField("References"))
    if loo_Refs.Length > 0 then
        loo_Refs.Append(" ")
    end if

    loo_Refs.Append(ls_ParentId)
    loo_Reply.AddHeaderField("References",loo_Refs.GetAsString())
end if

//  A short text reply has no use for the original inline artwork or nested correspondence.
loo_Reply.RemoveAttachedMessages()
loo_Reply.RemoveHtmlAlternative()
loo_Reply.DropRelatedItems()
loo_Reply.SetTextBody("Thank you for your message.","text/plain")
if loo_Reply.NumTo < 1 then
    Write-Debug "Reply has no destination. Review the original sender headers."
    destroy loo_Original
    destroy loo_Reply
    destroy loo_Refs
    return
end if

Write-Debug "Reply goes to: " + loo_Reply.GetToAddr(0)
Write-Debug "In-Reply-To: " + loo_Reply.GetHeaderField("In-Reply-To")

li_Success = loo_Reply.SaveEml("reply.eml")
if li_Success <> 1 then
    Write-Debug loo_Reply.LastErrorText
    destroy loo_Original
    destroy loo_Reply
    destroy loo_Refs
    return
end if

Write-Debug "Saved reply.eml."


destroy loo_Original
destroy loo_Reply
destroy loo_Refs