Send emails with Java API

Reliable transactional email delivery for your Java applications.

MailerSend

Java email API you can integrate in minutes

Easily integrate email into your standalone, web, or mobile application with a well–documented Java SDK that fits right in with your existing tech stack.
Read our API docs
import com.mailersend.sdk.Email;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public void sendEmail() {

    Email email = new Email();

    email.setFrom("name", "your email");
    email.addRecipient("name", "your@recipient.com");

    // you can also add multiple recipients by calling addRecipient again
    email.addRecipient("name 2", "your@recipient2.com");

    // there's also a recipient object you can use
    Recipient recipient = new Recipient("name", "your@recipient3.com");
    email.AddRecipient(recipient);
    
    email.setSubject("Email subject");

    email.setPlain("This is the text content");
    email.setHtml("This is the HTML content");

    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {    
        MailerSendResponse response = ms.emails().send(email);
        System.out.println(response.messageId);
    } catch (MailerSendException e) {
        e.printStackTrace();
    }
}
MailerSend

We’ve got you covered with SMTP support

Fast, secure, reliable SMTP relay for developers who prefer to use SMTP with an existing Java framework, like Spring or Jakarta, or need it for legacy systems. 

MailerSend

Instant setup with automatic domain verification

Get SPF, DKIM and DMARC set up as quickly as possible with the domain connect wizard. Follow the steps and we’ll automatically create your domain’s DNS authentication records. 

Build less, deliver more with dynamic templates

Build templates with dynamic content and variables to send personalized messages at scale. Create templates quickly or let designers take the lead with the drag & drop editor.
Read our API docs
import com.mailersend.sdk.emails.Email;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.Recipient;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public class MailerSendExample {

    public static void main(String[] args) {
        sendEmail(); // Call the sendEmail method
    }

    public static void sendEmail() {

        // Retrieve the API token from the environment variable
        String apiToken = System.getenv("MAILERSEND_API_KEY");

        // Check if the environment variable is set
        if (apiToken == null || apiToken.isEmpty()) {
            System.err.println("❌ API token not found. Please set the MAILERSEND_API_KEY environment variable.");
            return; // Exit the method if the token is missing
        }

        // Initialize the MailerSend instance with the API token from the environment variable
        MailerSend ms = new MailerSend();
        ms.setToken(apiToken);

        // Create the email object
        Email email = new Email();
        email.setFrom("Your name", "Your email");
        email.addRecipient("Recipient name", "name@example.com");

        // Or use the recipient object 
        Recipient recipient = new Recipient("Name 2", "name2@example.com");
        email.AddRecipient(recipient);


        // Set the MailerSend template ID
        email.setTemplateId("3z7vklo8jng47qkge");

        email.addPersonalization("name", "John");
        email.addPersonalization("email", "john@example.com");

        try {
            // Send the email
            MailerSendResponse response = ms.emails().send(email);
            System.out.println("✅ Email sent! Message ID: " + response.messageId);
        } catch (MailerSendException e) {
            System.err.println("❌ Failed to send email:");
            e.printStackTrace();
        }
    }
}
MailerSend

Straightforward integration of incoming messages

Use inbound routing to receive and parse incoming messages before sending them to an email address or endpoint. Improve workflows, integrate content into apps or CRMs, and create email-based functionality for users. 

Hassle-free attachments

Attach files from your filesystem and use the attachFile method to send invoices, PDFs, images and more with just a few lines of code.
Read our API docs
import com.mailersend.sdk.emails.Email;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

import java.io.File;
import java.io.IOException;

public class MailerSendExample {

    public static void main(String[] args) {
        sendEmail(); 
    }

    public static void sendEmail() {

        String apiToken = System.getenv("MAILERSEND_API_KEY");
        if (apiToken == null || apiToken.isEmpty()) {
            System.err.println("❌ API token not found. Please set the MAILERSEND_API_KEY environment variable.");
            return; // Exit the method if the token is missing
        }

        MailerSend ms = new MailerSend();
        ms.setToken(apiToken);

        Email email = new Email();
        email.setFrom("Your name", "Your email");
        email.addRecipient("Name", "name@example.com");

        email.setSubject("This is a test");
        email.setPlain("This is the text content for the test email with an attachment.");
        email.setHtml("

This is the HTML content for the test email with an attachment.

"); // Attach a file from the filesystem File file = new File("invoice.pdf"); try { if (file.exists()) { email.attachFile(file); } else { System.err.println("❌ Attachment file not found: invoice.pdf"); return; } } catch (IOException e) { System.err.println("❌ Failed to attach file:"); e.printStackTrace(); return; } try { MailerSendResponse response = ms.emails().send(email); System.out.println("✅ Email sent! Message ID: " + response.messageId); } catch (MailerSendException e) { System.err.println("❌ Failed to send email:"); e.printStackTrace(); } } }

Technical support when you need it

Our friendly and knowledgeable customer support team looks at each and every case, always aiming to provide the best solutions in record time. Get help via email or live chat.

93% satisfaction rate
100% response rate
24/7 support hours
I would recommend MailerSend because of the great support I got every time I struggled with something. Fast response times and competent help is my main experience so far. The API documentation and integration are straightforward. Templates can be handled by marketing without developer skills.
Norbert N. CTO
Before MailerSend, we were always worried that emails would go to spam or promotions, but now our clients sometimes ask "How did your FIRST email get into my inbox with a star on it?". For us, email is critical for the businesses that we help and MailerSend plays a critical role. Better still, they have humans that respond and read what you write to them.
Rodrigo S. CEO
Very good tool, it took me just one day to get it working on production. The interface is clean with everything exactly where expected. It works fantastically—emails are delivered immediately and as configured (conditional statements saved my life, no need to create lots of templates). The API documentation is also fantastic, plus the support team is really fast! 10 out of 10.
Roger S. -

Shall we give this a go?

Sign up to MailerSend for free and test out our email API with a trial domain. If you like what you see, subscribe to a free Hobby plan and get 3,000 emails/month included. 

What is the MailerSend Java SDK and why should I use it?

The MailerSend Java SDK is the official Java library that allows for quick and easy integration of MailerSend’s mail API for Java applications. It’s a solid solution that offers high deliverability, a user-friendly interface for teams, dev-friendly tools, in-depth documentation, and plenty of code examples developers can use to get started.

What kind of support is available for the MailerSend Java SDK?

Email support is available for all MailerSend plans while live chat support is available for Starter plans and above.

What MailerSend features can I access with this Java SDK?

MailerSend’s Java email API SDK includes access to a wide range of features including email sending, attachments, template management, inbound routing, domain management, SMS and much more. View the Java documentation.

Is there a free tier or trial available for MailerSend to test the SDK?

Absolutely! You can sign up for free and use the free trial with a testing domain to try out the API and SMTP. You can subscribe at any time to a free Hobby plan, which includes 3,000 emails per month. Check out our plans and pricing.