gpt4 book ai didi

javax.mail.MessagingException : Can't send command to SMTP host

转载 作者:行者123 更新时间:2023-12-02 03:58:39 35 4
gpt4 key购买 nike

当我尝试从 Java 邮件 API 发送邮件时遇到以下异常: javax.mail.MessagingException: Can't send command to SMTP host;
nested exception is: javax.net.ssl.SSLHandshakeException: com.ibm.jsse2.util.j: PKIX path building failed: java.security.cert.CertPathBuilderException: PKIXCertPathBuilderImpl could not build a valid CertPath.; internal cause is:
java.security.cert.CertPathValidatorException: The certificate issued by CN=Baltimore CyberTrust Root, OU=CyberTrust, O=Baltimore, C=IE is not trusted; internal cause is:
java.security.cert.CertPathValidatorException: Certificate chaining error

下面是我的Java代码。相同的代码在我的本地系统上运行良好。当我在 tomcat 服务器上发布 Web 应用程序但相同的代码无法工作时。当我在 IBM WebSphere Application Server 上部署它时。

它显示了上述异常,

public static void sendMail(String toMailIds,String subject,String htmlMsg){
String[] recipientList = toMailIds.split(",");
InternetAddress[] recipientAddress = new InternetAddress[recipientList.length];
int counter = 0;
for (String recipient1 : recipientList) {
try {
recipientAddress[counter] = new InternetAddress(recipient1.trim());
} catch (AddressException e) {
e.printStackTrace();
}
counter++;
}

// Sender's email ID needs to be mentioned
String from = "abc@xyz.com";
final String username = "abc@xyz.com";//change accordingly
final String password ="password`enter code here`";//change accordingly

String host = "smtp.office365.com";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.trust", host);
// props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.user", username);



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

try {
Transport tr = session.getTransport("smtp");
tr.connect();
System.out.println("Validating email finished");
// Create a default MimeMessage object.
Message message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress(from));

// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO, recipientAddress);

// Set Subject: header field
message.setSubject(subject);

// HTML TEXT
message.setContent(htmlMsg, "text/html");


// Send message
Transport.send(message);

System.out.println("Sent message successfully....");

} catch (Exception e) {
System.out.println("Exception--------------------"+e);
throw new RuntimeException(e);
}

// TODO Auto-generated constructor stub
}`

最佳答案

查看异常:

The certificate issued by CN=Baltimore CyberTrust Root, OU=CyberTrust, O=Baltimore, C=IE is not trusted; 

您使用 SSL 连接到 SMTP 服务器。您必须将 SMTP 服务器证书放入 WebSphere Application Truststore 才能建立连接。您的 Tomcat 服务器使用不同的 JDK,因此使用不同的信任库。

查看其他帖子如何将签名者证书添加到 WAS 中的信任库。

第二个考虑因素是您应该在 WAS 中使用 MailSession,而不是在代码中硬编码所有邮件服务器数据。这是在 Java EE 应用程序中获取邮件 session 的推荐方法。

如果您不想在完整的 WAS 上进行开发,那么您应该使用 WebSphere Liberty Profile 进行开发,而不是使用 Tomcat。它在启动时间和内存占用方面与 Tomcat 一样轻量级,并且 WAS 中已包含库,因此您无需添加第三方库。

关于javax.mail.MessagingException : Can't send command to SMTP host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35171412/

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