Comment on page

Email Definition

Create Emails
An email is defined as a simple POJO bean of type Email. Each part of the email message can be set individually. Moreover, Email supports a fluent interface, so even the definition of an e-mail message would look natural.
Email supports plain text, HTML messages, and any combination of both. When the only text or HTML message is set, a simple email will be sent. When both text and HTML message is set, or when attachments are added, a multi-part e-mail will be sent. Actually, Email supports any number of separate messages to be sent as an email. Here are some examples using fluent interface.
Plain-text email:
Email email = Email.create()
.textMessage("A plain text message...");
HTML email:
Email email = Email.create()
.subject("Hello HTML!")
.htmlMessage("<b>HTML</b> message...");
Text and HTML email, high priority:
Email email = Email.create()
.textMessage("text message...")
.htmlMessage("<b>HTML</b> message...")

Email Addresses

All email addresses (from, to, cc...) may be specified in the following ways:
  • only by email address, e.g.: [email protected]
  • by personal (display) name and email address in one string, e.g.: John <[email protected]>
  • by separate personal (display) name and email address
  • by providing EmailAddress, a class that parses and validates emails per specification
  • by providing InternetAddress or just Address instance.
Consider using personal names as it is less likely your message is going to be marked as spam ;)
Multiple email addresses are specified using arrays or by calling methods to() or cc() multiple times:
Email email = Email.create()
.subject("Hello HTML!")
.htmlMessage("<b>HTML</b> message");


There are several attachment types that can be added:
  • from a memory byte array,
  • from an input stream,
  • from a file,
  • from generic DataSource.
File attachments depend on javax.mail content type resolution (that might not work for you). You can always attach files as byte or input stream attachment.
Attachments are created using the EmailAttachment class:
.name("some name")
The content() method accepts different attachment types.

Embedded (inline) attachments

A special case of attachments is inline attachments. These are usually related content for HTML message, like images, that should appear inside the message, and not separate as a real attachment.
Embedding is also supported. All attachments created with the ContentID set will be considered as inline attachments. However, they also need to be embedded to a certain message, to form a so-called related part of an email. Email clients usually require to have all inline attachments related to some message.


Email email = Email.create()
"<html><META http-equiv=Content-Type content=\"text/html; " +
"charset=utf-8\"><body><h1>Hey!</h1><img src='cid:c.png'>" +
.content(new File("/c.png")))