本地服务器触发com.sun.mail.util.MailConnectException连接超时问题求助
com.sun.mail.util.MailConnectException for Gmail SMTP Let's walk through why you're hitting this connection timeout error and get your Java mail setup working again.
1. You're Using the Wrong SMTP Port for Gmail
First off, port 456 is not a valid SMTP port for Gmail—this is almost certainly the root cause of your timeout. Gmail uses two standard, supported ports:
587: For TLS encryption (requiresSTARTTLSenabled in your config)465: For direct SSL encryption
Swap out 456 for one of these two ports first, as this is the quickest fix for most cases.
2. Verify Network Connectivity from Your Localhost Server
Even with the right port, your localhost might not be able to reach Gmail's SMTP server. Test this with basic network tools to rule out network blocks:
- Ping the server:
ping smtp.gmail.com(should resolve to an IP and return responses) - Test port access:
telnet smtp.gmail.com 587(or 465). If this times out, you're dealing with a network restriction.
Common network issues here:
- Local firewall blocking outgoing traffic on 587/465
- Corporate/ISP network policies restricting mail ports
- No active internet connection from your localhost environment
3. Fix Your JavaMail Configuration
Make sure your JavaMail properties are set up correctly for Gmail. Here's a working example for port 587 (TLS, the recommended option):
import java.util.Properties; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; import javax.mail.Session; public class GmailMailSetup { public static Session getAuthenticatedSession() { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); // Correct TLS port props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); // Mandatory for port 587 props.put("mail.smtp.connectiontimeout", "5000"); // 5-second connection timeout props.put("mail.smtp.timeout", "5000"); // 5-second read timeout // Authenticate with Gmail credentials return Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // Use an App Password if you have 2FA enabled on your Google account return new PasswordAuthentication("your-email@gmail.com", "your-app-password"); } }); } }
Critical Authentication Notes for Gmail:
- If you have 2-Step Verification (2FA) enabled, you cannot use your regular password. Generate an App Password from your Google Account settings (under Security > App passwords).
- If 2FA is disabled, you may need to enable "Less secure app access" (though Google discourages this; App Passwords are the safer alternative).
4. Handle Proxies or Firewall Restrictions
If you're behind a corporate proxy or firewall:
- For SOCKS proxies, add these settings to your JavaMail properties:
props.put("mail.smtp.socks.host", "your-proxy-server"); props.put("mail.smtp.socks.port", "your-proxy-port"); - For HTTP proxies, set system properties when launching your Java app:
java -Dhttp.proxyHost=your-proxy -Dhttp.proxyPort=port -Dhttps.proxyHost=your-proxy -Dhttps.proxyPort=port YourMailApplication - Check with your network admin to confirm outgoing traffic on 587/465 is allowed.
5. Validate Timeout Settings
Your error mentions a timeout value of -1, which means no explicit timeout is configured. Adding connection and read timeouts (like in the example above) prevents your app from hanging indefinitely and provides clearer error feedback.
内容的提问来源于stack exchange,提问作者Alone King

