gpt4 book ai didi

java - 无法使用 zoho smtp 服务器发送邮件

转载 作者:行者123 更新时间:2023-11-29 04:26:50 25 4
gpt4 key购买 nike

我正在尝试使用以下代码使用 Zoho SMTP 服务器发送邮件:

 public void sendEmail(Email email) {
Properties props = setupMailEnv();

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("sssxxxx@xxx.com", "xxxxx");
}
});

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(email.getFromMailId()));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(email.getToEmailId()));
message.setSubject(email.getSubject());
message.setText(email.getBody());

Transport.send(message);

log.info("Mail Sent.");

} catch (MessagingException e) {
throw e;
}

}

private Properties setupMailEnv() {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.zoho.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.startssl.enable", "true");
props.put("mail.smtp.starttls.enable", "false");
return props;
}

当我执行这个程序时,它给出了以下异常:

javax.mail.MessagingException: Can't send command to SMTP host;
nested exception is:
java.net.SocketException: Connection closed by remote host

我引用了 Zoho 论坛,但没有一个提供解决方案。我该如何解决这个问题?

最佳答案

我使用了以下属性来调试错误:

props.put("mail.debug", "true");

问题出在发件人地址上。发件人地址应与以下地址匹配:

new PasswordAuthentication("sssxxxx@xxx.com", "xxxxx");

我更改了它,一切都按预期正常工作。

关于java - 无法使用 zoho smtp 服务器发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45846521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com