1. Products
  2.   Email
  3.   Java
  4.   JavaMail
 
  

Free Java API for Sending Simple & HTML-based Emails

Open Source Java Email Processing API to Create and Send Text and HTML-based Email Messages with Attachments, Embedded Images, SMTP, IMAP, POP3 Protocol Support abd Advanced Security support.

What is JavaMail?

In the world of enterprise applications and automated systems, the ability to send and receive emails programmatically is not just a convenience—it's a necessity. From sending password resets and order confirmations to processing incoming support tickets, email integration is a core feature. For Java developers, the cornerstone of this functionality has been the robust, open-source JavaMail API. This comprehensive product page will explore the JavaMail API, its powerful features, and provide practical code examples to get you started with email integration in your Java applications.

The JavaMail API is a mature, open source framework provided by Oracle (formerly Sun Microsystems) that provides a platform-independent and protocol-independent framework for building mail and messaging applications. It is the standard API for handling email in the Java ecosystem. It abstracts the complexities of underlying email protocols like SMTP, POP3, and IMAP, allowing developers to work with a clean, object-oriented interface. There are several important features part of the library, such as creating and sending HTML emails, adding attachments, inserting inline images, multi-part content, strong authentication and security support, folder-based email support, better email searching and filtering support, reading emails via SMTP, and so on.

Previous Next

Getting Started with JavaMail

First of all, you need to install JDK 1.6 or higher. You need to add the following maven dependency in pom.xml.

Maven Dependency

<dependency>
  <groupId>com.sun.mail</groupId>s;
  <artifactId>javax.mail</artifactId>
  <version>1.6.2</version>
</dependency>

You can download the library directly from GitHub page.

Email Message Sending via Java Library

The open source JavaMail library has included support for creating and sending simple as well as HTML email messages inside Java applications. Software developers can read existing emails, add file as well images as attachments, send email to multiple users, add custom headers, and many more. You need to define connection parameters like the host, port, and flags to enable authentication and TLS. The following example demonstrates how to send a basic plain-text email using an SMTP server (like Gmail's) inside Java applications.

How to Send a Basic Plain-Text Email using an SMTP Server via Java API?


import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class SimpleEmailSender {

    public static void main(String[] args) {
        // Sender's and recipient's email ID
        String from = "your.email@gmail.com";
        String to = "recipient.email@example.com";
        
        // SMTP server configuration (for Gmail)
        String host = "smtp.gmail.com";
        final String username = "your.email@gmail.com";
        final String password = "your-app-password"; // Use an App Password for Gmail

        // Setup mail server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true"); // Use TLS
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", "587");

        // Get the Session object and pass username and password
        Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {
            // Create a default MimeMessage object
            Message message = new MimeMessage(session);
            
            // Set From: header field
            message.setFrom(new InternetAddress(from));
            
            // Set To: header field
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            
            // Set Subject: header field
            message.setSubject("Hello from JavaMail API");
            
            // Set the actual message body
            message.setText("This is a test email sent programmatically using the JavaMail API.");

            // Send message
            Transport.send(message);
            System.out.println("Email sent successfully!");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

Email Searching & Filtering via java

The open source JavaMail library has included full support for searching email messages in a folder by various criteria (sender, subject, date, flags) inside java applications. This is especially useful when developing email clients or automations. You can also combine multiple search terms (AND, OR, NOT) via AndTerm, OrTerm, NotTerm. The following example demonstrates, how to search for messages from a specific sender using Java commands.

How to Search for Messages from a Specific Sender inside java Apps?


Store store = session.getStore("imap");
store.connect("imap.example.com", username, password);

Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);

// Search for messages from a specific sender
SearchTerm senderTerm = new FromStringTerm("alerts@example.com");
Message[] found = inbox.search(senderTerm);

for (Message m : found) {
    System.out.println("Subject: " + m.getSubject());
}

inbox.close(false);
store.close();

SMTP, IMAP, POP3 Protocol Support

The open source JavaMail by default supports the three most common email protocols, such as SMTP (Simple Mail Transfer Protocol) for sending messages, POP3 (Post Office Protocol 3) for simple email retrieval and IMAP (Internet Message Access Protocol) for more advanced email access (folders, partial fetch). Moreover, it also supports secure variants like SMTPS, POP3S, IMAPS, and can be extended to custom providers.

Advanced Authentication and Security via Java

The open source JavaMail supports secure communication with email servers, which is crucial in today's security-conscious environment. The library support TLS (Transport Layer Security) which encrypts the communication channel between your application and the mail server. It also supports SSL (Secure Sockets Layer) which is the predecessor to TLS, also supported for establishing a secure connection. The library also supports standard username/password authentication to connect to servers that require login.