无法使用Play 2.5

时间:2016-07-21 15:15:29

标签: java javamail playframework-2.5

我最近将我的应用程序从播放2.3迁移到本指南后的2.5版https://www.playframework.com/documentation/2.5.x/Migration25 我也根据这里指定https://github.com/playframework/play-mailer

的内容更新播放邮件

现在我的应用程序无法再发送电子邮件了。我创建了一个用于注入邮件程序实例的Injector构造函数

@Inject public SmtpConnector(MailerClient mailer) 
{ 
    this.mailerClient = mailer; 
    MailcapCommandMap mc = (MailcapCommandMap)   MailcapCommandMap.getDefaultCommandMap(); 
    mc.addMailcap("text/html;; x-java-content-  handler=com.sun.mail.handlers.text_html"); 
    mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); 
    mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
    mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
    mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
    CommandMap.setDefaultCommandMap(mc); 
}

我在异步任务中执行send email方法:

CompletableFuture.runAsync( () -> mailerClient.send(email) ).exceptionally(exc -> {exc.printStackTrace(); return null;});

但是我一直对mimetype multipart缺乏支持感到错误:

  

...   java.util.concurrent.CompletableFuture $ AsyncRun.run(CompletableFuture.java:1626)... 5更多引起:javax.mail.MessagingException:发送消息时发生IOException;嵌套异常是:javax.activation.UnsupportedDataTypeException:没有对象DCH的MIME类型multipart / alternative; border =“---- = _ Part_0_1284684208.1469102367572”at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1177)at javax.mail.Transport.send0(Transport.java:195)at javax.mail .Transport.send(Transport.java:124)at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1411)... 14更多引起:javax.activation.UnsupportedDataTypeException:没有MIME类型的对象DCH多部分/替代; border =“---- = _ Part_0_1284684208.1469102367572”at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:896)at javax.activation.DataHandler.writeTo(DataHandler.java:317)at javax.mail.internet.MimeBodyPart .writeTo(MimeBodyPart.java:1485)at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1773)at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1121)... 17更多

可是:

这有解决方法吗?是否有(另一种)方法使用带有Play框架2.5的java api发送电子邮件?

更新1:

根据jmehrens的建议,我激活了调试标志,这就是我所得到的:

  

无法加载DCH com.sun.mail.handlers.multipart_mixed;   例外:java.lang.ClassNotFoundException:com / sun / mail / handlers / multipart_mixed

根据这个github.com/playframework/play-mailer/issues/104,问题似乎是由于没有从Play控制器的上下文中调用该方法而触发

更新2 : 在这里你可以找到从开始到异常的-verbose:class输出:s000.tinyupload.com/?file_id = 50323853839855936002

更新3

在这里你可以找到jmehrens建议的getClass()转储的类加载器:s000.tinyupload.com/?file_id = 51200633758480523188

1 个答案:

答案 0 :(得分:2)

这解决了问题:

CompletableFuture.runAsync( () -> {
Thread.currentThread().setContextClassLoader( getClass().getClassLoader() ); 
mailerClient.send(email);}, Executors.newSingleThreadExecutor()).exceptionally(exc ->  {
exc.printStackTrace(); 
return null;});