Sample code for 30+ languages & platforms
Java

Add File Attachments to an Email

Demonstrates how to add one or more file attachments to an email.

Chilkat Java Downloads

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

public class ChilkatExample {

    public static void main(String[] args) {
        Email email = new Email();

        email.setSubject("This is a test");
        email.setBody("This is a test");
        email.setFrom("support@chilkatsoft.com");
        email.addTo("Chilkat Admin", "admin@chilkatsoft.com");

        // To add file attachments to an email, call AddFileAttachment
        // once for each file to be attached.  The method returns
        // the content-type of the attachment if successful, otherwise
        // returns cknull
        String contentType = "";

        try {
            contentType = email.addFileAttachment("something.pdf");

            contentType = email.addFileAttachment("something.xml");

            contentType = email.addFileAttachment("something.zip");

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

        System.out.println("Saved EML!");
    }
}