You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用javax.mail在25端口启用SSL发送邮件失败求助

Troubleshooting javax.mail SSL Failure on Port 25

Hey there, let's break down why your email send is failing and fix it step by step.

Core Issue

SMTP port 25 is not designed for direct SSL connections—it's meant for plaintext connections that can be upgraded to encryption using STARTTLS. For direct SSL-based SMTP, the standard port is 465 (often called SMTPS). Your current configuration forces SSL on port 25, which most mail servers don't support, leading to the failure. Also, there's a tiny syntax error in your socket factory class line (extra closing quote) that might be compounding issues.

Fix Options

Option 1: Switch to Port 465 (Direct SSL)

If your email provider supports port 465 (most major providers do), adjust your properties like this:

Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.auth", "true");
// Modern JavaMail versions might not need these socket factory settings, but they're safe to keep
properties.put("mail.smtp.socketFactory.fallback", "false");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // Fixed the quote issue

Authenticator auth = new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("your-email@domain.com", "your-password");
    }
};

Option 2: Use STARTTLS on Port 25

If you must use port 25 (e.g., due to network restrictions), use STARTTLS to upgrade the plaintext connection to encrypted. Replace your SSL-related properties with STARTTLS settings:

Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.starttls.enable", "true"); // Enable STARTTLS instead of direct SSL
properties.put("mail.smtp.starttls.required", "true"); // Force encryption, fail if upgrade isn't possible
properties.put("mail.smtp.auth", "true");

Authenticator auth = new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("your-email@domain.com", "your-password");
    }
};

Note: For this to work, your mail server must support STARTTLS on port 25.

Additional Checks

  • Verify that your email provider allows SMTP access from your network (some block ports 25/465 for non-trusted IPs).
  • Double-check your username and password—typos here are an extremely common culprit.
  • Ensure you're using a recent version of javax.mail (or jakarta.mail, its modern successor) to avoid outdated SSL compatibility bugs.

内容的提问来源于stack exchange,提问作者Vinayak CD

火山引擎 最新活动