繁体   English   中英

Java 邮件:发送email时异常

[英]Java Mail: Exception when sending email

第一次使用 java 邮件。 我正在按照本教程进行操作,但我已经无法发送基本消息,并且收到一个非常奇怪的错误:

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 服务器是正确的并且可以正常工作。

有什么建议为什么会发生以及我该如何解决?

编辑:显然地址/主机在这里发生了变化,我使用的是在实际代码中工作的真实地址/主机。

事实证明我遇到了多个问题:

  1. 教程问题

它使用com.sun.mail.smtp.SMTPMessage但在我的情况下它不起作用但使用javax.mail.internet.MimeMessage工作正常。

  1. 错误的根本原因

以上代码在基于 3rd 方 eclipse 的应用程序中运行,它们似乎相互干扰。 可以在此处找到解决方案:

Thread t =  Thread.currentThread();
ClassLoader ccl = t.getContextClassLoader();
t.setContextClassLoader(session.getClass().getClassLoader());
try {
    Transport.send(m);
} finally {
    t.setContextClassLoader(ccl);
}

相应地调整代码使其工作。

这是发送带附件的多部分消息的示例:

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();
}

希望能帮助到你。

我在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查看所有依赖项及其依赖项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM