Sample code for 30+ languages & platforms
CkJava Requires Chilkat v11.0.0+

Forward an email as an attachment

See more Email Object Examples

Attach the original Email to a new message as a message/rfc822 part. The recipient can open the attached email and read its original headers and body.

Chilkat CkJava Downloads

CkJava
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

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

    CkEmail original = new CkEmail();
    success = original.LoadEml("message.eml");
    if (success != true) {
        System.out.println(original.lastErrorText());
        return;
        }

    CkEmail email = new CkEmail();
    email.put_From("Sam Lee <sam@example.com>");
    success = email.AddTo("Alex Rivera","alex@example.net");
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

    email.put_Subject("Forwarded message");
    email.put_Body("The original email is attached.");
    success = email.AttachEmail(original);
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

    success = email.SaveEml("forward-as-attachment.eml");
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

    System.out.println("Saved forward-as-attachment.eml.");
  }
}