Java邮件:发送邮件时出现异常
Java Mail: Exception when sending email
第一次使用 java 邮件。我正在关注 this tutorial,但我已经无法发送基本消息,并且收到一个非常奇怪的错误:
java.util.ServiceConfigurationError: javax.mail.Provider: Provider com.sun.mail.imap.IMAPProvider not a subtype
奇怪,因为我没有在我的代码中的任何地方使用 IMAP:
Properties mailProps = new Properties();
mailProps.put("mail.transport.protocol", "smtp");
mailProps.put("mail.host", "smtp.mydomain.com");
mailProps.put("mail.from", "me@mydomain.com");
mailProps.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(mailProps);
SMTPMessage m = new SMTPMessage(session);
MimeMultipart content = new MimeMultipart();
MimeBodyPart mainPart = new MimeBodyPart();
mainPart.setText("test");
content.addBodyPart(mainPart);
m.setContent(content);
m.setSubject("Demo message");
m.setRecipient(RecipientType.TO, new InternetAddress("john@example.com"));
Transport.send(m);
错误发生在最后一行(发送)。我知道 smtp 服务器是正确的并且可以正常工作。
关于为什么会发生这种情况以及我该如何解决的任何建议?
编辑:显然 addresses/hosts 在这里发生了变化,我使用的是在实际代码中工作的真实代码。
这是发送带有附件的多部分消息的示例:
String from = "from@example.com";
String to = "to@example.com";
File file = new File("/file/to/attach");
Properties mailProps = new Properties();
// put your properties here
Session session = Session.getInstance(mailProps, null);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from) );
InternetAddress[] toAddress = { new InternetAddress(to) };
message.setRecipients(Message.RecipientType.TO, toAddress);
message.setSubject("Demo Message");
message.setSentDate(new Date());
MimeBodyPart part1 = new MimeBodyPart();
part1.setText("Test");
MimeBodyPart part2 = new MimeBodyPart();
part2.attachFile(file);
Multipart multiPart = new MimeMultipart();
multiPart.addBodyPart(part1);
multiPart.addBodyPart(part2);
message.setContent(multiPart);
Transport.send(message);
} catch( MessagingException e ) {
// handle the exception properly
e.printStackTrace();
}
希望对您有所帮助。
事实证明我 运行 遇到了多个问题:
- 教程有问题
它使用 com.sun.mail.smtp.SMTPMessage
但在我的情况下不起作用,但使用 javax.mail.internet.MimeMessage
可以正常工作。
- 错误的根本原因
以上代码在基于 3rd 方 eclipse 的应用程序中运行,它们似乎相互干扰。可以找到解决方案 :
Thread t = Thread.currentThread();
ClassLoader ccl = t.getContextClassLoader();
t.setContextClassLoader(session.getClass().getClassLoader());
try {
Transport.send(m);
} finally {
t.setContextClassLoader(ccl);
}
相应地调整代码使其工作。
我在 libertyCore 服务器上遇到了同样的问题
我在 server.xml 中添加了这个功能,它对我有用
<feature>javaMail-1.6</feature>
java.util.ServiceConfigurationError: jakarta.mail.Provider: com.sun.mail.imap.IMAPProvider not a subtype
我遇到了类似的问题,因为项目正在使用jakarta.mail
;并且其中一个依赖项带来了 javax.mail
作为传递依赖项。
排除旧的 javax.mail
应该可以解决问题。
<dependency>
<groupId>org.abc</groupId>
<artifactId>xyz</artifactId>
<version>0.0.0</version>
<exclusions>
<exclusion>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</exclusion>
</exclusions>
</dependency>
mvn dependency:tree
查看所有依赖项及其依赖项。
第一次使用 java 邮件。我正在关注 this tutorial,但我已经无法发送基本消息,并且收到一个非常奇怪的错误:
java.util.ServiceConfigurationError: javax.mail.Provider: Provider com.sun.mail.imap.IMAPProvider not a subtype
奇怪,因为我没有在我的代码中的任何地方使用 IMAP:
Properties mailProps = new Properties();
mailProps.put("mail.transport.protocol", "smtp");
mailProps.put("mail.host", "smtp.mydomain.com");
mailProps.put("mail.from", "me@mydomain.com");
mailProps.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(mailProps);
SMTPMessage m = new SMTPMessage(session);
MimeMultipart content = new MimeMultipart();
MimeBodyPart mainPart = new MimeBodyPart();
mainPart.setText("test");
content.addBodyPart(mainPart);
m.setContent(content);
m.setSubject("Demo message");
m.setRecipient(RecipientType.TO, new InternetAddress("john@example.com"));
Transport.send(m);
错误发生在最后一行(发送)。我知道 smtp 服务器是正确的并且可以正常工作。
关于为什么会发生这种情况以及我该如何解决的任何建议?
编辑:显然 addresses/hosts 在这里发生了变化,我使用的是在实际代码中工作的真实代码。
这是发送带有附件的多部分消息的示例:
String from = "from@example.com";
String to = "to@example.com";
File file = new File("/file/to/attach");
Properties mailProps = new Properties();
// put your properties here
Session session = Session.getInstance(mailProps, null);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from) );
InternetAddress[] toAddress = { new InternetAddress(to) };
message.setRecipients(Message.RecipientType.TO, toAddress);
message.setSubject("Demo Message");
message.setSentDate(new Date());
MimeBodyPart part1 = new MimeBodyPart();
part1.setText("Test");
MimeBodyPart part2 = new MimeBodyPart();
part2.attachFile(file);
Multipart multiPart = new MimeMultipart();
multiPart.addBodyPart(part1);
multiPart.addBodyPart(part2);
message.setContent(multiPart);
Transport.send(message);
} catch( MessagingException e ) {
// handle the exception properly
e.printStackTrace();
}
希望对您有所帮助。
事实证明我 运行 遇到了多个问题:
- 教程有问题
它使用 com.sun.mail.smtp.SMTPMessage
但在我的情况下不起作用,但使用 javax.mail.internet.MimeMessage
可以正常工作。
- 错误的根本原因
以上代码在基于 3rd 方 eclipse 的应用程序中运行,它们似乎相互干扰。可以找到解决方案
Thread t = Thread.currentThread();
ClassLoader ccl = t.getContextClassLoader();
t.setContextClassLoader(session.getClass().getClassLoader());
try {
Transport.send(m);
} finally {
t.setContextClassLoader(ccl);
}
相应地调整代码使其工作。
我在 libertyCore 服务器上遇到了同样的问题 我在 server.xml 中添加了这个功能,它对我有用
<feature>javaMail-1.6</feature>
java.util.ServiceConfigurationError: jakarta.mail.Provider: com.sun.mail.imap.IMAPProvider not a subtype
我遇到了类似的问题,因为项目正在使用jakarta.mail
;并且其中一个依赖项带来了 javax.mail
作为传递依赖项。
排除旧的 javax.mail
应该可以解决问题。
<dependency>
<groupId>org.abc</groupId>
<artifactId>xyz</artifactId>
<version>0.0.0</version>
<exclusions>
<exclusion>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</exclusion>
</exclusions>
</dependency>
mvn dependency:tree
查看所有依赖项及其依赖项。