【发布时间】:2023-03-28 02:10:02
【问题描述】:
我正在尝试使用 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 论坛,但没有一个给出解决方案。我该如何解决这个问题?
【问题讨论】:
标签: java email jakarta-mail zoho