You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用JSP+JavaBean发送确认邮件时遇SMTP连接错误求助

Hey there! Let's troubleshoot that SMTP connection error you're hitting with Gmail — I’ve dealt with this exact issue a few times, so here are the most likely fixes to get your emails sending:

1. Fix Gmail’s Security Settings (the #1 Culprit)

Gmail blocks "less secure" apps by default now, which is almost certainly part of the problem:

  • Enable 2-Step Verification & Use an App Password: If your Gmail account has 2FA turned on (which it should!), generate a dedicated App Password (under your Google Account > Security > App Passwords) and use that instead of your regular login password in your code. This is the safest and recommended approach.
  • Temporary Workaround (Not Recommended Long-Term): If you don’t want to enable 2FA, you can toggle on "Less secure app access" in your Google Account settings — but note that Google is phasing out this option, so it’s only a short-term fix.

2. Double-Check Your SMTP Configuration Parameters

For Gmail’s SSL port (465), make sure your Properties are set correctly — typos here are super common:

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

If you’d rather use TLS (port 587, which some networks allow when 465 is blocked), switch to this config:

props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");

3. Rule Out Network/Firewall Blocks

Your server or local machine might be blocking outbound traffic on port 465. Test connectivity with this command:

telnet smtp.gmail.com 465

If it fails to connect, reach out to your network admin to open the port, or switch to the TLS port 587 as mentioned above.

4. Validate Your JavaMail Dependencies

Make sure you’re using compatible versions of the JavaMail API (and JAF, if you’re on older Java versions). Mixing old/new jars can cause weird connection issues. For modern projects, use the jakarta.mail dependency instead of the legacy javax.mail if you’re on Java EE 9+.

5. Verify Your Authentication Code

Double-check that your Session is using the correct Authenticator with the right credentials:

Session session = Session.getInstance(props, new javax.mail.Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("your-full-email@gmail.com", "your-app-password");
    }
});

Pro tip: Enable debug mode to see detailed connection logs — this will help you spot exactly where things are breaking:

session.setDebug(true);

Give these steps a try, and let me know if you’re still stuck!

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:01:52