If you searched for , the practical production answer is:
- use a maintained mail client library (Jakarta Mail)
- configure SMTP auth and TLS explicitly
- externalize transport settings per environment
- verify inbox outcomes with integration tests
This page replaces legacy Java SMTP snippets with a release-ready setup pattern.
Quick setup
Add Jakarta Mail dependency:
Minimal Java SMTP send:
SMTP settings Java teams should lock down
For reliable sends, define and validate:
- SMTP host and port
- auth mechanism and credential rotation policy
- TLS mode (
and trust settings) - sender-domain alignment for SPF, DKIM, and DMARC
- timeout/retry behavior for temporary delivery failures
Related references:
Environment-driven Java SMTP configuration
Use environment variables to avoid hardcoded config drift:
This pattern makes staging/production parity and secret rotation easier to manage.
Common Java SMTP failures and fixes
Authentication failures ( or auth-related exceptions)
Likely causes:
- stale credentials
- unsupported auth mode
- sender-domain policy mismatch
Fix by validating SMTP credentials and sender alignment together.
TLS/handshake errors
Likely causes:
- wrong port/TLS mode pairing
- certificate/trust-chain issues
- blocked outbound network egress
Fix by validating transport mode, trust settings, and network policy as one check.
SMTP accepted but inbox missing message
Transport success does not guarantee inbox placement.
Add deliverability and placement checks:
Test Java SMTP sends before release
Run deterministic tests for:
- signup and verification mail
- password reset and magic-link flows
- billing and receipt notifications
- alerting and escalation messages
Recommended workflow pages:
Java SMTP production checklist
- Keep SMTP host/port/auth/TLS in env-managed config.
- Validate SMTP auth and sender-domain posture together.
- Use deterministic inbox assertions for critical user flows.
- Add retry and timeout policies for transient SMTP failures.
- Re-run deliverability checks after template or DNS changes.
FAQ
Should I use Jakarta Mail or older JavaMail ()?
For current projects, Jakarta Mail is the maintained path. Legacy apps may still run , but modernization reduces long-term risk.
What is the safest default Java SMTP port setup?
Commonly port 587 with STARTTLS, but always follow your provider-specific requirements and verify in each environment.
How do I validate Java SMTP in CI?
Use inbox-based integration tests that assert message receipt and content for each release-critical workflow.