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

C
#include <C_CkEmail.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmail original;
    HCkEmail reply;
    const char *destination;
    const char *parentId;
    HCkStringBuilder refs;

    success = FALSE;

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

    original = CkEmail_Create();
    success = CkEmail_LoadEml(original,"with-attachments.eml");
    if (success != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(original));
        CkEmail_Dispose(original);
        return;
    }

    reply = CkEmail_Create();
    success = CkEmail_ToReply(original,reply);
    if (success != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(original));
        CkEmail_Dispose(original);
        CkEmail_Dispose(reply);
        return;
    }

    CkEmail_putFrom(reply,"Alex Rivera <alex@example.net>");
    //  This is a reply to the author, not a reply-all. Set the destination explicitly.
    CkEmail_ClearTo(reply);
    CkEmail_ClearCC(reply);
    CkEmail_ClearBcc(reply);
    destination = CkEmail_getHeaderField(original,"Reply-To");
    if (destination == "") {
        destination = CkEmail_ck_from(original);
    }

    success = CkEmail_AddMultipleTo(reply,destination);
    if (success != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(reply));
        CkEmail_Dispose(original);
        CkEmail_Dispose(reply);
        return;
    }

    //  Link this answer to its parent and preserve the earlier conversation references.
    parentId = CkEmail_getHeaderField(original,"Message-ID");
    if (parentId != "") {
        CkEmail_AddHeaderField(reply,"In-Reply-To",parentId);
        refs = CkStringBuilder_Create();
        CkStringBuilder_Append(refs,CkEmail_getHeaderField(original,"References"));
        if (CkStringBuilder_getLength(refs) > 0) {
            CkStringBuilder_Append(refs," ");
        }

        CkStringBuilder_Append(refs,parentId);
        CkEmail_AddHeaderField(reply,"References",CkStringBuilder_getAsString(refs));
    }

    //  A short text reply has no use for the original inline artwork or nested correspondence.
    CkEmail_RemoveAttachedMessages(reply);
    CkEmail_RemoveHtmlAlternative(reply);
    CkEmail_DropRelatedItems(reply);
    CkEmail_SetTextBody(reply,"Thank you for your message.","text/plain");
    if (CkEmail_getNumTo(reply) < 1) {
        printf("Reply has no destination. Review the original sender headers.\n");
        CkEmail_Dispose(original);
        CkEmail_Dispose(reply);
        CkStringBuilder_Dispose(refs);
        return;
    }

    printf("Reply goes to: %s\n",CkEmail_getToAddr(reply,0));
    printf("In-Reply-To: %s\n",CkEmail_getHeaderField(reply,"In-Reply-To"));

    success = CkEmail_SaveEml(reply,"reply.eml");
    if (success != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(reply));
        CkEmail_Dispose(original);
        CkEmail_Dispose(reply);
        CkStringBuilder_Dispose(refs);
        return;
    }

    printf("Saved reply.eml.\n");


    CkEmail_Dispose(original);
    CkEmail_Dispose(reply);
    CkStringBuilder_Dispose(refs);

    }