Sending emails in Java should be easier. Jodd provides some nice classes for sending and receiving emails in an easier, practical way.

E-mail definition

Email is defined as simple POJO bean of type Email. Each part of email message can be set separately. Moreover, Email supports fluent interface, so even definition of an e-mail message would look natural.

Email supports plain text, HTML messages and any combination of both. When only text or HTML message is set, simple email will be sent. When both text and HTML message is set, or when attachments are added, multipart 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 following ways:

  • only by email address, e.g.:
  • by personal (display) name and email address in one string, e.g.: John <>
  • by separate personal (display) name and email address
  • by providing EmailAddress, 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()
        .to("", "")
        .subject("Hello HTML!")
        .htmlMessage("<b>HTML</b> message");


There are several attachment types that can be added:

  • from memory byte array,
  • from 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

Special case of attachments are inline attachments. These are usually related content for HTML message, like images, that should appear inside the message, and not separate as 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 certain message, to form a so-called related part of an email. Email clients usually requires 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")))

Sending emails

Emails are sent using SendMailSession. Mail session encapsulates process of preparing emails, opening and closing transport connection and sending emails.

Mail session is created by the MailServer.

    SmtpServer smtpServer = MailServer.create()
    SendMailSession session = smtpServer.createSession();;

Since opening session and sending emails may produce EmailException, it is necessary to wrap methods in try-catch block and closing the session in the finally block.

Sending using SSL

Preferred way for sending e-mails is using SSL protocol. Jodd supports secure e-mail sending. Just set the ssl() flag while creating the server.

Here is an example of sending e-mail via Gmail (port 465 is set by default):

    SmtpServer smtpServer = MailServer.create()
            .auth("", "password")
    SendMailSession session = smtpServer.createSession();;

Everything is the same, just different session provider is used.

Receiving emails

Receiving emails is similar to sending: there are classes that encapsulates POP3 and IMAP connections, i.e. servers. Both creates the same receiving session - ReceiveMailSession - that fetches emails and return them as an array of ReceivedEmails. This way you work with both POP3 and IMAP servers in the very same way.

Even the instances of the same class ReceiveMailSession are created by POP3 and IMAP servers implementations, not all methods work in the same way! This difference depends on a server type. Commonly, POP3 has less features (e.g. not being able to fetch all folder names for GMail account), while IMAP server is richer (e.g. it supports server-side search).

During receiving, all emails are fetched and returned as an array of ReceivedEmail objects. This is a POJO object, so its very easy to work with. It provides many helpful methods, too. Each ReceivedEmail also contains a list of all messages, attachments and attached messages (EMLs).

Receiving Builder

The proposed method to fetch emails is to use the builder. It is simple, powerful and you can fine-tune the receiving process. For example, you can mark or unmark some flags, select folder and so on. Here is how:


Convenient methods

ReceiveMailSession contains some legacy but convenient methods for fetching emails:

  • receiveEmail() - returns all emails, but don't change the 'seen' flag.
  • receiveEmailAndMarkSeen() - returns all emails and marked all messages as 'seen'.
  • receiveEmailAndDelete() - returns all emails and mark them as 'seen' and 'deleted'.

The first method does a little trick: since javax.mail always set a 'seen' flag when new message is downloaded, we do set it back on 'unseen' (if it was like that before fetching). This way receiveEmail() should not change the state of your inbox.


For POP3 connection, use Pop3Server:

    Pop3Server popServer = MailServer.create().
            auth("username", "password")

    ReceiveMailSession session = popServer.createSession();;
    ReceivedEmail[] emails = session.receiveEmailAndMarkSeen();
    if (emails != null) {
        for (ReceivedEmail email : emails) {
            System.out.println("\n\n===[" + email.getMessageNumber() + "]===");

            // common info
            System.out.println("FROM:" + email.from());
            System.out.println("TO:" +[0]);
            System.out.println("SUBJECT:" + email.subject());
            System.out.println("PRIORITY:" + email.priority());
            System.out.println("SENT DATE:" + email.sentDate());
            System.out.println("RECEIVED DATE: " + email.receiveDate());

            // process messages
            List messages = email.getAllMessages();
            for (EmailMessage msg : messages) {

            // process attachments
            List<EmailAttachment> attachments = email.getAttachments();
            if (attachments != null) {
                for (EmailAttachment attachment : attachments) {
                    System.out.println("name: " + attachment.getName());
                    System.out.println("cid: " + attachment.getContentId());
                    System.out.println("size: " + attachment.getSize());
                        new File("d:\\", attachment.getName()));

Receiving emails using SSL

Again, very simply: use the very same ssl() flag. Here is how it can be used to fetch email from Google:

    Pop3Server popServer = MailServer.create()
        .auth("username", "password")

    ReceiveMailSession session = popServer.createSession();;


Above example can be converted to IMAP usage very easily:

    ImapServer imapServer = MailServer.create()
        .auth("username", "password")

    ReceiveMailSession session = imapServer.createSession();;

Can't be easier:)

As said above, when working with IMAP server, many methods of ReceiveMailSession works better or… simply, just works. For example, you should be able to use following methods:

  • getUnreadMessageCount() - to get number of un-seen messages.
  • getAllFolders() - to receive all folders names
  • server-side filtering - read the next chapter

Receiving Envelopes

There is an option to receive only email envelopes: the header information, like from and subject but not the content of the messages. Receiving only envelopes makes things faster and it make more sense in situations when not all messages have to be received.

Each email has it's own ID that is fetched as well. Later on, you can use this ID to filter out just the messages with specific ID.

Filtering emails

IMAP server also knows about server-side filtering of emails. There are two ways how to construct email filter, and both can be used in the same time.

The first approach is by grouping terms:


Static method filter() is a factory of EmailFilter instances. Above filter defines the following query expressions:

    (from:"from" AND to:"to") OR not(subject:"subject") OR from:"from"

With this approach you can define boolean queries of any complexity. But there is a more fluent way to write the same query:


Here we use non-argument methods to define current boolean operator: and() and or(). All terms defined after the boolean marker method uses that boolean operator. Method not() works only for the very next term definition. This way you probably can not defined some complex queries, but it should be just fine for the real life usages.

Here is how we can use simple filter when fetching emails:

    ReceivedEmail[] emails = session.receiveEmail(filter()
            .flag(Flags.Flag.SEEN, false)

This would return all unread messages with subject equals to "Hello".

Note that for IMAP server, the search is executed by the IMAP server. This significantly speeds up the fetching process as not all messages are downloaded. Note that searching capabilities of IMAP servers may vary.

You can use the same filters on POP3 server, but keep in mind that the search is performed on the client-side, so still all messages have to be downloaded before the search is thrown.