使用AWS SES企业账号发送邮件时遇无法连接主机错误
Let's walk through the key checks and fixes to resolve that Couldn't connect to host, port: email-smtp.us-east-1.amazonaws.com, 587; timeout -1 error you're facing:
1. Verify Your AWS Region & SMTP Endpoint Match
Double-check that your Amazon SES service is actually enabled in the us-east-1 (N. Virginia) region. If you set up SES in a different region (like us-west-2), you'll need to swap the HOST value to the corresponding SMTP endpoint for that region. For example, us-west-2 uses email-smtp.us-west-2.amazonaws.com.
2. Rule Out Network/Firewall Blockages
This is the most common cause of connection timeouts. Your company's network firewall or proxy might be blocking outbound traffic to port 587 (SES's SMTP port). To test this:
- Run a quick connectivity test from your machine:
telnet email-smtp.us-east-1.amazonaws.com 587ornc -zv email-smtp.us-east-1.amazonaws.com 587 - If the test fails, reach out to your IT team to whitelist the SES SMTP endpoint and port 587 for outbound traffic.
3. Confirm You're Using the Correct SES SMTP Credentials
Make sure you didn't mix up AWS IAM access keys with SES SMTP credentials. SES generates separate SMTP usernames and passwords (found in the SES Console under "SMTP Settings" > "Create My SMTP Credentials"). IAM keys won't work for SMTP authentication—this is a super common mistake!
4. Add Timeout Parameters to Your Java Mail Configuration
Your current code doesn't set connection/transport timeouts, which can lead to hanging connections. Add these lines to your Properties setup to get clearer error feedback faster:
props.put("mail.smtp.connectiontimeout", "5000"); // 5-second connection timeout props.put("mail.smtp.timeout", "5000"); // 5-second transport timeout props.put("mail.smtp.writetimeout", "5000"); // 5-second write timeout
5. Check SES Sandbox Mode Restrictions
If you're still in SES Sandbox mode (the default for new accounts), you can only send emails to verified email addresses/domains. Even if your domain is verified, double-check that the recipient roger@myorg.com is also verified in the SES Console. If you need to send to unverified recipients, you'll need to request to move out of Sandbox mode.
6. Validate Your Configuration Set (If Used)
Your code references a CONFIGSET named "ConfigSet". Ensure this configuration set exists in the us-east-1 region of your SES account. While this won't cause a connection timeout, it's a good idea to rule out any secondary issues once you fix the connection problem.
Quick Code Adjustment
Here's how your updated Properties block would look with timeout parameters added:
Properties props = System.getProperties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.port", PORT); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.auth", "true"); // Add timeout settings props.put("mail.smtp.connectiontimeout", "5000"); props.put("mail.smtp.timeout", "5000"); props.put("mail.smtp.writetimeout", "5000");
Start with the network connectivity test and credential check—those are the most likely culprits here.
内容的提问来源于stack exchange,提问作者roger_that

