gpt4 book ai didi

Java Mail API 间歇性失败

转载 作者:行者123 更新时间:2023-12-02 03:56:33 25 4
gpt4 key购买 nike

我使用java mail api创建了自动电子邮件报告,需要在批处理文件运行后每天触发。虽然它在大多数情况下工作正常,但有时它会给出异常javax.mail.MessagingException:无法连接到SMTP主机:我的主机名,端口:25;嵌套异常是:java.net.ConnectException:连接被拒绝。这不是由于身份验证问题,因为我每次发送电子邮件时都使用相同的凭据。

我不确定为什么 java mail api 间歇性失败。我可以获得一些调试问题的建议吗?

我正在使用下面的代码片段 -

  String to = "xyz@gmail.com";//change accordingly

String from = "abc@mydomain.com";//change accordingly
final String username = "abc";//change accordingly
final String password = "*****";//change accordingly

String host = "My SMTP server";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");

// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Testing Subject");
message.setText("message to stakeholders");
Transport.send(message);
}

最佳答案

最好检查邮件服务器上的日志,以了解拒绝的可能原因 - 特别是因为您知道尝试失败的时间。

关于Java Mail API 间歇性失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35391637/

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