Skip to main content
Use Jakarta Mail, the official Java mail API, to send email through mySMTP with STARTTLS on port 587. The reference implementation is Angus Mail.

Prerequisites

  • Java 11 or later
  • Maven or Gradle
  • Your mySMTP username and password from the setup email
  • Your mySMTP hostname from the setup email (shown as relayxx.mysmtp.com in these examples)

Install Angus Mail

Basic send example

Replace relayxx.mysmtp.com with your real hostname from the mySMTP setup email. Load credentials from environment variables, never hardcode them in source files.

HTML email with attachment

If you use Spring Boot, add these settings to application.properties:
Inject JavaMailSender and call send(SimpleMailMessage) or send(MimeMessage).

Common errors

Your username or password is incorrect, or your account is not yet activated. Re-enter credentials exactly as shown in the setup email. See Setting up SMTP credentials.
Your network or host may block port 587. Try port 2525 with the same properties, or port 465 with mail.smtp.ssl.enable=true. See Supported SMTP ports and encryption.
The encryption properties do not match the port. Use mail.smtp.starttls.enable=true for ports 587 and 2525. Use mail.smtp.ssl.enable=true for port 465.
Test your SPF, DKIM, and DMARC setup with the mySMTP Deliverability Toolbox before sending large volumes.