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

Add files to a draft as attachments

See more Email Object Examples

Use explicit MIME types for a PDF agenda and an XML schedule. Filenames are relative to the application working directory. Save after both files have been added so a missing file does not leave a partially prepared draft.

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 email = new CkEmail();
    email.put_From("Workshop team <events@example.com>");
    success = email.AddTo("Alex Rivera","alex@example.net");
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

    email.put_Subject("Workshop registration");
    email.put_Body("The agenda and machine-readable schedule are attached.");
    success = email.AddFileAttachment2("agenda.pdf","application/pdf");
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

    success = email.AddFileAttachment2("schedule.xml","application/xml");
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

    System.out.println("Attachments in draft: " + email.get_NumAttachments());

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

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