Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oOriginal
LOCAL oReply
LOCAL cDestination
LOCAL cParentId
LOCAL oRefs

nSuccess := 0

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

oOriginal := CreateObject("Chilkat.Email")
nSuccess := oOriginal:LoadEml("with-attachments.eml")
IF (nSuccess != 1)
    ? oOriginal:LastErrorText
    oOriginal:destroy()
    RETURN
ENDIF

oReply := CreateObject("Chilkat.Email")
nSuccess := oOriginal:ToReply(oReply)
IF (nSuccess != 1)
    ? oOriginal:LastErrorText
    oOriginal:destroy()
    oReply:destroy()
    RETURN
ENDIF

oReply:From := "Alex Rivera <alex@example.net>"
//  This is a reply to the author, not a reply-all. Set the destination explicitly.
oReply:ClearTo()
oReply:ClearCC()
oReply:ClearBcc()
cDestination := oOriginal:GetHeaderField("Reply-To")
IF (cDestination == "")
    cDestination := oOriginal:From
ENDIF

nSuccess := oReply:AddMultipleTo(cDestination)
IF (nSuccess != 1)
    ? oReply:LastErrorText
    oOriginal:destroy()
    oReply:destroy()
    RETURN
ENDIF

//  Link this answer to its parent and preserve the earlier conversation references.
cParentId := oOriginal:GetHeaderField("Message-ID")
IF (cParentId != "")
    oReply:AddHeaderField("In-Reply-To", cParentId)
    oRefs := CreateObject("Chilkat.StringBuilder")
    oRefs:Append(oOriginal:GetHeaderField("References"))
    IF (oRefs:Length > 0)
        oRefs:Append(" ")
    ENDIF

    oRefs:Append(cParentId)
    oReply:AddHeaderField("References", oRefs:GetAsString())
ENDIF

//  A short text reply has no use for the original inline artwork or nested correspondence.
oReply:RemoveAttachedMessages()
oReply:RemoveHtmlAlternative()
oReply:DropRelatedItems()
oReply:SetTextBody("Thank you for your message.", "text/plain")
IF (oReply:NumTo < 1)
    ? "Reply has no destination. Review the original sender headers."
    oOriginal:destroy()
    oReply:destroy()
    oRefs:destroy()
    RETURN
ENDIF

? "Reply goes to: " + oReply:GetToAddr(0)
? "In-Reply-To: " + oReply:GetHeaderField("In-Reply-To")

nSuccess := oReply:SaveEml("reply.eml")
IF (nSuccess != 1)
    ? oReply:LastErrorText
    oOriginal:destroy()
    oReply:destroy()
    oRefs:destroy()
    RETURN
ENDIF

? "Saved reply.eml."

oOriginal:destroy()
oReply:destroy()
oRefs:destroy()