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

Create an email with plain-text and HTML bodies

See more Email Object Examples

Set the From and Reply-To addresses, add a recipient, and provide plain-text and HTML versions of the same body. Save the message as text-and-html.eml.

Chilkat Java Downloads

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

public class ChilkatExample {

    public static void main(String[] args) {
        // Assumes Chilkat is unlocked. See Global Unlock Sample.

        Email email = new Email();
        email.setFromName("Sam Lee");
        email.setFromAddress("sam@example.com");
        email.setReplyTo("replies@example.com");
        try {
            email.addTo("Alex Rivera", "alex@example.net");

            email.setSubject("Sample message");
            email.setCharset("utf-8");
            // A custom header can carry a reference alongside the standard message fields.
            email.addHeaderField("X-Reference-ID", "REF-42");
            email.addPlainTextAlternativeBody("Read more at https://example.com/details.");

            email.addHtmlAlternativeBody("<p><a href='https://example.com/details'>Read more</a></p>");

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

        System.out.println("Saved text-and-html.eml.");
    }
}