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

Unicode C++
#include <CkEmailW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkEmailW original;
    success = original.LoadEml(L"with-attachments.eml");
    if (success != true) {
        wprintf(L"%s\n",original.lastErrorText());
        return;
    }

    CkEmailW reply;
    success = original.ToReply(reply);
    if (success != true) {
        wprintf(L"%s\n",original.lastErrorText());
        return;
    }

    reply.put_From(L"Alex Rivera <alex@example.net>");
    //  This is a reply to the author, not a reply-all. Set the destination explicitly.
    reply.ClearTo();
    reply.ClearCC();
    reply.ClearBcc();
    const wchar_t *destination = original.getHeaderField(L"Reply-To");
    if (destination == L"") {
        destination = original.ck_from();
    }

    success = reply.AddMultipleTo(destination);
    if (success != true) {
        wprintf(L"%s\n",reply.lastErrorText());
        return;
    }

    //  Link this answer to its parent and preserve the earlier conversation references.
    const wchar_t *parentId = original.getHeaderField(L"Message-ID");
    if (parentId != L"") {
        reply.AddHeaderField(L"In-Reply-To",parentId);
        CkStringBuilderW refs;
        refs.Append(original.getHeaderField(L"References"));
        if (refs.get_Length() > 0) {
            refs.Append(L" ");
        }

        refs.Append(parentId);
        reply.AddHeaderField(L"References",refs.getAsString());
    }

    //  A short text reply has no use for the original inline artwork or nested correspondence.
    reply.RemoveAttachedMessages();
    reply.RemoveHtmlAlternative();
    reply.DropRelatedItems();
    reply.SetTextBody(L"Thank you for your message.",L"text/plain");
    if (reply.get_NumTo() < 1) {
        wprintf(L"Reply has no destination. Review the original sender headers.\n");
        return;
    }

    wprintf(L"Reply goes to: %s\n",reply.getToAddr(0));
    wprintf(L"In-Reply-To: %s\n",reply.getHeaderField(L"In-Reply-To"));

    success = reply.SaveEml(L"reply.eml");
    if (success != true) {
        wprintf(L"%s\n",reply.lastErrorText());
        return;
    }

    wprintf(L"Saved reply.eml.\n");
    }