Sample code for 30+ languages & platforms
Java

Send HTML Email with Attachments

See more SMTP Examples

Demonstrates how to send an HTML email with file attachments.

Chilkat Java Downloads

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

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        MailMan mailman = new MailMan();

        mailman.setSmtpHost("smtp.my-starttls-mail-server.com");

        mailman.setSmtpUsername("MY-SMTP-USERNAME");
        mailman.setSmtpPassword("MY-SMTP-PASSWORD");

        mailman.setStartTLS(true);
        mailman.setSmtpPort(587);

        // Create a new email object
        Email email = new Email();
        email.setSubject("Test SMTP API to Send HTML Email with Attachments");
        email.setFrom("Joe Programmer <joe@my-starttls-mail-server.com>");
        email.addTo("Chilkat Support", "support@chilkatsoft.com");

        // Add a plain-text alternative body, which will likely never be seen.
        // (It is shown if the receiving email client is incapable of displaying HTML email.)
        email.addPlainTextAlternativeBody("This is a plain-text alternative body...");

        // Our HTML will include an image, so add the related image here.
        String contentIdStarfish;
        try {
            contentIdStarfish = email.addRelatedFile("qa_data/jpg/starfish.jpg");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The src attribute for the image tag is set to the contentIdStarfish:
        StringBuilder sbHtml = new StringBuilder();
        sbHtml.append("<html><body><p>This is an HTML email with an embedded image.</p>");
        sbHtml.append("<p><img src=\"cid:CONTENT_ID_STARFISH\" /></p></body></html>");
        int numReplacements = sbHtml.replace("CONTENT_ID_STARFISH", contentIdStarfish);

        email.addHtmlAlternativeBody(sbHtml.getAsString());

        // Finally, add some attachments to the email.
        // Add a file attachment.
        try {
            email.addFileAttachment2("qa_data/pdf/fishing.pdf", "application/pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Add an attachment where the content is contained in a string.
        String content = "This is the content of the 2nd attached file.";
        email.addStringAttachment("someText.txt", content);

        // Send the HTML email.
        try {
            mailman.sendEmail(email);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        try {
            mailman.closeSmtpConnection();
        } catch (ChilkatException e) {
            System.out.println("Connection to SMTP server not closed cleanly.");
        }

        System.out.println("HTML email with attachments sent!");
    }
}