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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoOriginal
    Variant vReply
    Handle hoReply
    String sDestination
    String sParentId
    Handle hoRefs
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatEmail)) To hoOriginal
    If (Not(IsComObjectCreated(hoOriginal))) Begin
        Send CreateComObject of hoOriginal
    End
    Get ComLoadEml Of hoOriginal "with-attachments.eml" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoOriginal To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatEmail)) To hoReply
    If (Not(IsComObjectCreated(hoReply))) Begin
        Send CreateComObject of hoReply
    End
    Get pvComObject of hoReply to vReply
    Get ComToReply Of hoOriginal vReply To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoOriginal To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Set ComFrom Of hoReply To "Alex Rivera <alex@example.net>"
    //  This is a reply to the author, not a reply-all. Set the destination explicitly.
    Send ComClearTo To hoReply
    Send ComClearCC To hoReply
    Send ComClearBcc To hoReply
    Get ComGetHeaderField Of hoOriginal "Reply-To" To sDestination
    If (sDestination = "") Begin
        Get ComFrom Of hoOriginal To sDestination
    End

    Get ComAddMultipleTo Of hoReply sDestination To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoReply To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Link this answer to its parent and preserve the earlier conversation references.
    Get ComGetHeaderField Of hoOriginal "Message-ID" To sParentId
    If (sParentId <> "") Begin
        Send ComAddHeaderField To hoReply "In-Reply-To" sParentId
        Get Create (RefClass(cComChilkatStringBuilder)) To hoRefs
        If (Not(IsComObjectCreated(hoRefs))) Begin
            Send CreateComObject of hoRefs
        End
        Get ComGetHeaderField Of hoOriginal "References" To sTemp1
        Get ComAppend Of hoRefs sTemp1 To iSuccess
        Get ComLength Of hoRefs To iTemp1
        If (iTemp1 > 0) Begin
            Get ComAppend Of hoRefs " " To iSuccess
        End

        Get ComAppend Of hoRefs sParentId To iSuccess
        Get ComGetAsString Of hoRefs To sTemp1
        Send ComAddHeaderField To hoReply "References" sTemp1
    End

    //  A short text reply has no use for the original inline artwork or nested correspondence.
    Send ComRemoveAttachedMessages To hoReply
    Send ComRemoveHtmlAlternative To hoReply
    Send ComDropRelatedItems To hoReply
    Send ComSetTextBody To hoReply "Thank you for your message." "text/plain"
    Get ComNumTo Of hoReply To iTemp1
    If (iTemp1 < 1) Begin
        Showln "Reply has no destination. Review the original sender headers."
        Procedure_Return
    End

    Get ComGetToAddr Of hoReply 0 To sTemp1
    Showln "Reply goes to: " sTemp1
    Get ComGetHeaderField Of hoReply "In-Reply-To" To sTemp1
    Showln "In-Reply-To: " sTemp1

    Get ComSaveEml Of hoReply "reply.eml" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoReply To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Saved reply.eml."


End_Procedure