Problem
How do I set up outbound SMTP using JavaMail?
Background
JavaMail (part of the javax.mail API) is a standard Java library for sending and receiving email using internet protocols such as SMTP, POP3 and IMAP. It does not include its own mail server; instead, your application must connect to an outgoing SMTP server to deliver messages. JavaMail is commonly used in web applications, services, and backend processes where automated email delivery is required.
Configuring JavaMail correctly means specifying the SMTP host, port, authentication settings and any required encryption (TLS or SSL). If these settings are incorrect or if the chosen SMTP server applies restrictions your application may fail to send email or messages may be flagged as spam or rejected by the recipient’s mail server.
A reliable SMTP relay is essential for automated or programmatic email sending, particularly when sending from a custom domain or from servers with dynamic IP addresses.
Solution
The following example of code shows you how to send an email using outMail as the SMTP SmartHost mail relay using the JavaMail for Java.
outMail is an authenticated SMTP relay so the example below shows authentication in Java as well. The code below uses the default SMTP port 25 but it can easily be changed to an alternative SMTP port by editing the variable hostPort in the code below.
Using outMail as the outgoing mail server is an ideal fit for JavaMail-based applications. outMail provides authenticated SMTP access, supports secure TLS encryption, and is designed to work reliably with programmatic email sending.
Because outMail is not tied to a specific ISP or network, Java applications can send email consistently from any environment — including cloud servers, containers, and on-premise systems. This helps avoid common issues such as blocked ports, relay restrictions, or messages being rejected by recipient mail servers.
Example code smtp-test.java
import java.io.*;
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SmtpTest {
public static void main(String[] args) {
// outMail login Details
final String outmailUsername = "outmail-username";
final String outmailPassword = "outmail-password";
// Recipient's Email Address
String toEmail = "recipient@example.com";
// Sender's Email Address
String fromEmail = "me@example.com";
String fromEmailName = "Firstname Lastname";
// Set the email properties
Properties props = new Properties();
props.put("mail.smtp.host", "mxXXXXXX.smtp-engine.com"); // SMTP Host
props.put("mail.smtp.port", "25"); // SMTP Port
props.put("mail.smtp.auth", "true"); // SMTP Authentication Enabled
// Create an authenticator object
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(outmailUsername, outmailPassword);
}
};
Session session = Session.getInstance(props, auth);
try
{
MimeMessage msg = new MimeMessage(session);
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setSentDate(new Date());
msg.setFrom(new InternetAddress(fromEmail, fromEmailName));
msg.setReplyTo(InternetAddress.parse(fromEmail, false));
msg.setSubject("Subject of the email goes here", "UTF-8");
msg.setText("Body of the message goes here", "UTF-8");
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
Transport.send(msg);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Additional Deliverability and DNS Considerations
When sending email programmatically using JavaMail, correct SMTP settings are only part of the picture. To improve delivery success and reduce the likelihood of messages being marked as spam, you should also ensure that your domain is configured with proper email authentication records.
SPF (Sender Policy Framework)
SPF allows domain owners to list which mail servers are authorised to send email on behalf of the domain. Adding an SPF record that includes your outbound SMTP service helps receiving mail servers verify legitimate senders.
DKIM (DomainKeys Identified Mail)
DKIM applies a digital signature to outgoing messages, verifying that the content hasn’t been tampered with in transit. This provides another layer of authentication that receiving servers can check.
DMARC (Domain-based Message Authentication, Reporting & Conformance)
DMARC builds on SPF and DKIM by specifying how receiving servers should handle messages that fail authentication. It also provides reporting, so you can monitor authentication results and adjust your setup if needed.
Together, SPF, DKIM and DMARC help increase your domain’s sender reputation, improve inbox placement, and reduce the risk of emails being rejected or routed to junk folders. outMail can DKIM sign emails, please see the managament portal for configuring DKIM.
Looking for an outgoing #SMTP server?
outMail is an outbound #SMTP Server.
- Supports SMTP & API protocols for sending emails.
- Support for SMTP ports and alternative ports.
- Authenticated SMTP
- Support for SPF & DKIM
- Easy setup
Summary of server details
|
Outgoing server |
mxXXXXXX.smtp-engine.com As provided in your signup email. |
|
Outgoing server protocol |
SMTP |
|
Outgoing server port |
25, 465, 587, 2525 or 8025 |
|
Authentication Type |
Basic Authentication, SSL and TLS supported |
|
Username |
As provided |
|
Password |
As provided |


