我尝试使用Gmail SMTP向任何Gmail帐户发送简单的电子邮件。得到以下错误。
{"scope":"https://uri.paypal.com/services/subscriptions https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://uri.paypal.com/services/applications/webhooks openid https://uri.paypal.com/services/invoicing https://api.paypal.com/v1/vault/credit-card/.*","access_token":"xxx","token_type":"Bearer","app_id":"APP-xxx","expires_in":28800}
我的代码是
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
nested exception is:
java.net.ConnectException: Connection timed out: connect
答案 0 :(得分:1)
检查您的端口。来自Google的支持:
如果您尝试在端口465(使用SSL)和端口587(使用TLS)上配置SMTP服务器,但仍然无法发送邮件,请尝试将SMTP配置为使用端口25(使用SSL)。
来源:https://support.google.com/mail/answer/78775?hl=en
所以,尝试使用端口25,看看会发生什么。
答案 1 :(得分:1)
我已使用Apache Commons Email库通过java中的Gmail smtp发送电子邮件 documentation有一个很简单的例子:
Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();