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

Create, save, and reopen a plain-text email

See more Email Object Examples

Set the sender, recipient, subject, and plain-text body. Save the result as an EML file, then open it in a second Email object and read its contents.

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.setFrom("Sam Lee <sam@example.com>");
        try {
            email.addTo("Alex Rivera", "alex@example.net");

            email.setSubject("Hello");
            email.setBody("This is a plain-text message.");
            email.saveEml("message.eml");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Email received = new Email();
        try {
            received.loadEml("message.eml");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println(received.getFrom());
        System.out.println(received.getSubject());
        System.out.println(received.getPlainTextBody());
    }
}