Sample code for 30+ languages & platforms
Java

Send HTML Email with Image to iPhone

Demonstrates how to compose an HTML email with an embedded image that will display correctly on an iPhone. This example produces an email that looks like this on the iPhone:

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.

        // The mailman object is used for sending and receiving email.
        MailMan mailman = new MailMan();

        // Use your SMTP server hostname.  This example uses office365, but it could be any SMTP server..
        mailman.setSmtpHost("outlook.office365.com");
        mailman.setSmtpPort(587);
        mailman.setStartTLS(true);

        // Set the SMTP login/password
        mailman.setSmtpUsername("OFFICE365-SMTP-LOGIN");
        mailman.setSmtpPassword("OFFICE365-SMTP-PASSWORD");

        // Create a new email object
        Email email = new Email();
        email.setSubject("Testing for Chilkat SMTP API...");
        email.setBody("Testing for Chilkat SMTP API...");
        email.setFrom("Chilkat Support <my-office365-user@mydomain.com>");
        email.addTo("Chilkat Support", "support@chilkatsoft.com");

        // For whatever reason, the iPhone's email program requires
        // images in HTML to be referenced by Content-ID.  Therefore,
        // we must add the image like this:
        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.setHtmlBody(sbHtml.getAsString());

        try {
            mailman.sendEmail(email);
            System.out.println("Mail Sent!");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
        }
    }
}