Send SMTP email with Java

How to use Java SMTP client to send email with MailSlurp mail server on the JDK

java smtp

Java provides battle-tested mail sending capabilities with the javax.mail:mail package. Using this library we can connect to SMTP mailboxes and send emails using the SMTP transport. To send emails from MailSlurp accounts we can use the MailSlurp Java library to create an inbox and get the username, password, port and host for SMTP access.

See the JavaDocs documentation for more information.

Install maven dependencies

First let us install MailSlurp and the Java mail package using Maven.

<?xml version="1.0" encoding="UTF-8"?>

Setup mailbox access

Next we can create a new throw-away email inbox using MailSlurp. First we must set the API Key for the MailSlurp client.

private static ApiClient apiClient;
private static final Long TIMEOUT = 60000L;
private static final String apiKey = System.getenv("API_KEY");

public static void beforeAll() throws Exception {
    // get API KEY for mailslurp from environment variable
    if (StringUtils.isBlank(apiKey)) {
        throw new Exception("Must provide API KEY");

    // IMPORTANT set timeout for the http client
    OkHttpClient httpClient = new OkHttpClient.Builder()
            .connectTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
            .writeTimeout(TIMEOUT, TimeUnit.SECONDS)
            .readTimeout(TIMEOUT, TimeUnit.SECONDS)

    apiClient = Configuration.getDefaultApiClient();

    // IMPORTANT set api client timeouts

    // IMPORTANT set API KEY and client

Send email with SMTP mail transport

To send an email using Javax Mail package first create an inbox then get the inbox access details from MailSlurp. Use the returned imap smtp access details to configure the mail properties.

public void canSendWithSmtp() throws MessagingException, ApiException {
    CreateInboxDto opts = new CreateInboxDto();
    InboxControllerApi inboxControllerApi = new InboxControllerApi(apiClient);
    InboxDto inbox = inboxControllerApi.createInboxWithOptions(opts);

    ImapSmtpAccessDetails server = inboxControllerApi.getImapSmtpAccess(inbox.getId());

    Properties prop = new Properties();
    prop.put("mail.smtp.auth", "true");
    prop.put("mail.transport.protocol", "smtp");
    prop.put("mail.smtp.starttls.enable", "false");
    prop.put("", server.getSmtpServerHost());
    prop.put("mail.smtp.port", server.getSmtpServerPort());

    class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            String username = server.getSmtpUsername();
            String password = server.getSmtpPassword();
            return new PasswordAuthentication(username, password);

    Authenticator auth = new SMTPAuthenticator();
    Session mailSession = Session.getDefaultInstance(prop, auth);

    Message message = new MimeMessage(mailSession);
    message.setFrom(new InternetAddress(inbox.getEmailAddress()));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(inbox.getEmailAddress()));
    message.setSubject("Test subject");

    String msg = "This is my first email";

    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    mimeBodyPart.setContent(msg, "text/html; charset=utf-8");

    Multipart multipart = new MimeMultipart();



More options

For more email options with Java see the MailSlurp Java documentation and the examples repositories.

Related content

Fake SMTP servers for safe testing that prevent any outbound emails from reaching real users

Test emails safely with no risk of spamming users with real emails

Java email library for SMTP and creating email addresses

MailSlurp Java SDK for sending and receive email and attachments on the JVM.

SMTP email application libraries

How to send email in CSharp, PHP, Ruby, Java, Python, and NodeJS using the SMTP protocol and MailSlurp mailservers.

1111 SMTP Connections failing

1111 SMTP Connections failing

How to Send an Email in Python

Send emails using SMTP mailer in Python. Detailed tutorial for sending mail in Python.

How to send emails in Javascript (NodeJS)

JS SMTP email sending guide using HTML and NodeJS.

PHPMailer tutorial (Sending email with PHP)

PHPMailer tutorial (Sending email with PHP)

How to send an email using Powershell (Windows and cross-platform)

Use Send-MailMessage in Windows Powershell to send emails using an SMTP server or MailSlurp's free email API.

How to send email in R

RLang tutorial for sending emails over SMTP.

SMTP Commands and Response Codes List

A full list of commands and response status codes for SMTP mail servers when sending emails.

Stripe get all customers list (example)

Stripe get all customers list (example)

Test SMTP servers with Telnet

How to use Telnet to test the SMTP servers?

What Is Amazon Simple Storage Service (SES) - And How Can I Use It?

Amazon SES is an email service among virtual servers, AI apps, and libraries. Ideally, you can integrate your email system with your program hosted on Amazon.

Grab all details about an SMTP Port

What are common SMTP mail server ports and how do you find them?

Which SMTP Port Should I Use?

Picking the right port for SMTP mailservers

Java JVM Examples

Test email sending and receive emails without a mail server.

TestNG Selenium Java Example

Testing user sign up in Java using TestNG and MailSlurp test email accounts

Java, Selenium

Receive emails in Java test suites using MailSlurp, Junit, and Selenium.

Python Robot Framework email test

Python automation email testing Robotframework plugin

Test applications with real emails using Serenity BDD, JBehave and Selenium

Email acceptance testing with Serenity and MailSlurp. Test applications with real email addresses.

Avoiding email spam filters

How to send emails that don't trigger a spam warning.

BCC and CC in SMTP emails

What is cc and bcc in email? What is the difference between cc and bcc?

BCC For Email: What It Means And When To Use It

What is BCC email field (blind carbon copy) and how does it differ to CC?

What Is The Best SMTP Port To Use?

MailServers use TCP ports to transmit emails. Picking the right port between 25 vs 2555, 465 vs 587 in SMTP is easy with this guide.

How to configure Mail desktop clients with MailSlurp

How to setup MailSlurp with Outlook, Gmail, Thunderbird, Mail app and more.

Send email with CSharp using SMTP Client and MailSlurp

Create a custom SMTP client and access MailSlurp inboxes from CSharp/DotNET.

DMARC – All Facts that you should know

DMARC – All Facts that you should know

Email size limits

Email size limits

The Fundamentals Of Email Subdomains

How to manage email subdomain custom mailboxes using SMTP/IMAP mailserver settings.

How to send emails in VBA

Send email in Visual basic

A guide to PHP Mailer

A guide to PHP Mailer

SMTP IMAP Settings

How to access MailSlurp email accounts using IMAP and SMTP servers

SMTP Relay for outbound smtp servers

How to use SMTP relay to address spam and security issues that affect outbound mail servers.

SMTP vs HTTP MailSlurp inboxes

The difference between HTTP and SMTP inboxes and domains in MailSlurp.

What are SMTP ports

What are SMTP ports

CSharp send SMTP email

How to use CSharp SMTP client to send email with MailSlurp mail server

Send SMTP email with Java

How to use Java SMTP client to send email with MailSlurp mail server on the JDK

Send emails in NodeJS using SMTP

How to use Javascript SMTP client (or Nodemailer) to send email with MailSlurp mail server

Send SMTP emails with PHP

Use PHPMailer to send emails with SMTP and MailSlurp

Send SMTP email with Python (using SMTPLib)

How to send emails with Python's built-in SMTP client and MailSlurp mail servers.

Ruby send SMTP email

How to use Ruby SMTP client to send email with the MailSlurp mail servers

Ready to dive in?Start building email applications today.