gpt4 book ai didi

java - 通过 Java 发送邮件时出现异常

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:01:57 27 4
gpt4 key购买 nike

Java发送邮件时出现异常

异常(exception)是::

Exception in thread "main" java.lang.NoSuchMethodError: 
com.sun.mail.util.LineOutputStream.<init>(Ljava/io/OutputStream;Z)V
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1648)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1906)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1134)
at javax.mail.Transport.send0(Transport.java:255)
at javax.mail.Transport.send(Transport.java:124)
at com.yodlee.SendMail.sendMail(SendMail.java:78)
at com.yodlee.SendMail.main(SendMail.java:32)

这是 Java 代码::

    Properties props = new Properties();
props.put(SMTP_HOST_KEY,SMTP_HOST);

Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(".....",
"#.......");
}
});

try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(RECIPIENT_FROM));
message.setSubject(SUBJECT);
message.setSentDate(new Date());
//message.setText("Test mails111");


MimeBodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setContent(data, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
status = -1;
throw new RuntimeException(e);
}

我在 Transport.send(msg) 时遇到错误;我在这里使用的 jar ::smtp 1.6.0,Javax Mail 1.4.7,mailactivation

最佳答案

您的类路径中很可能有多个版本的 JavaMail 类。检查所有依赖项和应用程序服务器环境是否存在冲突。如果不止一个 jar 文件包含 javax.mail.* 或 com.sun.mail.* 类,您就有问题了。

并确保修复所有这些 common JavaMail mistakes , 并且一定要使用 latest version of JavaMail .

关于java - 通过 Java 发送邮件时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48276701/

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