我在使用 Gmail 电子邮件 SMTP 端口和 JakartaMail 发送电子邮件时遇到问题。
代码:
final String email = "[email protected]";
final String pass = "xxxxx";
public Email() {
this.callerObj = this;
this.msg = null;
this.mp = null;
this.props = null;
this.session = null;
this.props = new Properties();
this.props.setProperty("mail.smtp.auth", "true");
this.props.setProperty("mail.smtp.starttls.enable", "true");
this.props.setProperty("mail.smtp.host", "smtp.gmail.com");
this.props.setProperty("mail.smtp.port", "465");
this.props.setProperty("mail.debug", rbApp.getString("app.mail.debug"));
this.session = Session.getInstance(this.props, null);
}
.....
session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(email, pass);
}
});
....
错误信息:
MessagingException 捕获 >> 无法发送电子邮件 >> 无法连接 到 SMTP 主机:smtp.gmail.com,端口:465,响应:-1
这是我在 Thunderbird 中的 SMTP 服务器配置:
来自@AnFi,
更改为端口 587,而不是端口 465。(基于 starttls 和 SMTP 知识的猜测,没有 jakarta 知识)。
已成功连接到 Gmail SMTP 主机并发送电子邮件。