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

Visual FoxPro
LOCAL lnSuccess
LOCAL loOriginal
LOCAL loReply
LOCAL lcDestination
LOCAL lcParentId
LOCAL loRefs

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

loOriginal = CreateObject('Chilkat.Email')
lnSuccess = loOriginal.LoadEml("with-attachments.eml")
IF (lnSuccess <> 1) THEN
    ? loOriginal.LastErrorText
    RELEASE loOriginal
    CANCEL
ENDIF

loReply = CreateObject('Chilkat.Email')
lnSuccess = loOriginal.ToReply(loReply)
IF (lnSuccess <> 1) THEN
    ? loOriginal.LastErrorText
    RELEASE loOriginal
    RELEASE loReply
    CANCEL
ENDIF

loReply.From = "Alex Rivera <alex@example.net>"
*  This is a reply to the author, not a reply-all. Set the destination explicitly.
loReply.ClearTo()
loReply.ClearCC()
loReply.ClearBcc()
lcDestination = loOriginal.GetHeaderField("Reply-To")
IF (lcDestination = "") THEN
    lcDestination = loOriginal.From
ENDIF

lnSuccess = loReply.AddMultipleTo(lcDestination)
IF (lnSuccess <> 1) THEN
    ? loReply.LastErrorText
    RELEASE loOriginal
    RELEASE loReply
    CANCEL
ENDIF

*  Link this answer to its parent and preserve the earlier conversation references.
lcParentId = loOriginal.GetHeaderField("Message-ID")
IF (lcParentId <> "") THEN
    loReply.AddHeaderField("In-Reply-To",lcParentId)
    loRefs = CreateObject('Chilkat.StringBuilder')
    loRefs.Append(loOriginal.GetHeaderField("References"))
    IF (loRefs.Length > 0) THEN
        loRefs.Append(" ")
    ENDIF

    loRefs.Append(lcParentId)
    loReply.AddHeaderField("References",loRefs.GetAsString())
ENDIF

*  A short text reply has no use for the original inline artwork or nested correspondence.
loReply.RemoveAttachedMessages()
loReply.RemoveHtmlAlternative()
loReply.DropRelatedItems()
loReply.SetTextBody("Thank you for your message.","text/plain")
IF (loReply.NumTo < 1) THEN
    ? "Reply has no destination. Review the original sender headers."
    RELEASE loOriginal
    RELEASE loReply
    RELEASE loRefs
    CANCEL
ENDIF

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

lnSuccess = loReply.SaveEml("reply.eml")
IF (lnSuccess <> 1) THEN
    ? loReply.LastErrorText
    RELEASE loOriginal
    RELEASE loReply
    RELEASE loRefs
    CANCEL
ENDIF

? "Saved reply.eml."

RELEASE loOriginal
RELEASE loReply
RELEASE loRefs