Send emails with Java API
Reliable transactional email delivery for your Java applications.

Java email API you can integrate in minutes
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();
}
}

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.

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.
Everything you need to start sending
API reference
An in-depth guide to MailerSend API features and endpoints.
GitHub repository
Code snippets you can use to quickly start sending with the MailerSend API for Java.
Developer guide
Our technical guide to setting up the MailerSend Java email API SDK and sending your first email.
Java help article
An in-depth article with clear examples for sending emails with Java via API or SMTP.
Build less, deliver more with dynamic templates
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();
}
}
}

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
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();
}
}
}
Tools for smarter debugging
-
API & SMTP logs
Get full visibility into every request with searchable logs and template errors. -
Real-time activity dashboard
Monitor messages at every point of their journey, from Queued to Clicked, and catch bounces as they happen. -
Webhook notifications
Use webhooks to trigger notifications about all message events, including bounces, spam complaints, unsubscribes and more.



There’s an endpoint for that
Send order confirmations, password resets, OTPs and more.
/bulk-email
Send service updates, event reminders, and account summaries.
/activity
Create custom dashboards or integrate activity metrics into other apps.
/analytics
Automate reports, pull data into internal tools, or trigger conditional logic.
/domains
Automate domain setup and get an overview of active domains.
/inbound
Manage inbound routes and create user-specific routes on signup.
/recipients
Share recipient data across tools to sync contacts and manage suppressions.
/templates
Send with email templates that look professional and contain dynamic content.
/email-verification
Configure forms to perform real-time email validation or verify a whole list.
/sms
Send appointment reminders, 2FA messages, shipping updates and more.
/smtp-users
Programmatically create SMTP users during environment setup.
View more endpoints

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.
Dev-approved, team-friendly
Explore all features
Powerful RESTful API service

Start sending and tracking your emails with our easy API integration process and clean documentation.
Email verification

Verify a single email address or upload an entire email list to verify in bulk.
Webhooks

Get notified of email events to trigger automatic reactions.
Advanced email analytics

View your email activity in real-time to gain insights and make adjustments when needed.
Email inbound processing

Automatically parse incoming emails. Inbound email routes enable MailerSend to receive emails on your behalf, integrating them into your application.
Dynamic email templates

Build one-to-one customer relationships on a mass scale using a single email template.
And much more to deliver, every single time
Meet our SDKs
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.