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

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // Assumes Chilkat is unlocked. See Global Unlock Sample.

        Email original = new Email();
        try {
            original.loadEml("with-attachments.eml");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Email reply = new Email();
        try {
            original.toReply(reply);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        reply.setFrom("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();
        String destination = original.getHeaderField("Reply-To");
        if ("".equals(destination)) {
            destination = original.getFrom();
        }

        try {
            reply.addMultipleTo(destination);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Link this answer to its parent and preserve the earlier conversation references.
        String parentId = original.getHeaderField("Message-ID");
        if (!"".equals(parentId)) {
            reply.addHeaderField("In-Reply-To", parentId);
            StringBuilder refs = new StringBuilder();
            refs.append(original.getHeaderField("References"));
            if (refs.getLength() > 0) {
                refs.append(" ");
            }

            refs.append(parentId);
            reply.addHeaderField("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("Thank you for your message.", "text/plain");
        if (reply.getNumTo() < 1) {
            System.out.println("Reply has no destination. Review the original sender headers.");
            return;
        }

        System.out.println("Reply goes to: " + reply.getToAddr(0));
        System.out.println("In-Reply-To: " + reply.getHeaderField("In-Reply-To"));

        try {
            reply.saveEml("reply.eml");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Saved reply.eml.");
    }
}