01 Add the dependency
Install Simple Java Mail.
Start with simple-java-mail. It gives you the public API and everything needed for the usual send.
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>9.3.2</version>
</dependency>
Using Gradle?
implementation("org.simplejavamail:simple-java-mail:9.3.2")
Published on Maven Central. Keep the current Javadocs alongside the guide when adapting the examples.
02 Build a message
Describe the content and recipients.
Simple Java Mail chooses and builds the corresponding MIME structure.
Email email = EmailBuilder.startingBlank()
.from("Sender", "sender@example.org")
.withRecipients(new RecipientBuilder()
.withName("Recipient")
.withAddress("recipient@example.net")
.withType(TO)
.build())
.withSubject("It works")
.withPlainText("Your first Simple Java Mail message.")
.buildEmail();
03 Configure and send
Build one reusable mailer.
Keep credentials outside the source, and choose the transport strategy that matches your SMTP service.
Mailer mailer = MailerBuilder
.withSMTPServer(
System.getenv("SMTP_HOST"),
587,
System.getenv("SMTP_USER"),
System.getenv("SMTP_PASSWORD"))
.withTransportStrategy(TransportStrategy.SMTP_TLS)
.buildMailer();
mailer.sendMail(email);
Port and transport strategy must match the server. See the transport strategy guide before adapting this example.
04 Verify the setup
Test each part separately.
Test the connection
Check the server and credentials separately from a message send.
mailer.testConnection();
Validate the message
Check the supplied email's sender, recipients, addresses, and injection-sensitive values without submitting it.
mailer.validate(email);
An SMTP server accepting a submission does not guarantee final delivery or inbox placement. The diagnostics guide separates each stage.
Add only what you need
Optional modules
The main dependency covers everyday sending. Add a module only when the application needs the feature.
batch-modulePooled and clustered sendingConfigure →
authenticated-socks-moduleSOCKS proxy credentialsConfigure →
dkim-moduleDomain-key signingConfigure →
smime-moduleS/MIME signing and encryptionConfigure →
outlook-moduleOutlook MSG conversionConvert →
cli-moduleStandalone command-line useUse the CLI →
spring-moduleSpring configurationIntegrate →
karaf-moduleOSGi / Karaf deploymentSee modules →
Where to go next