发送多部分消息时发生Javamail ClassCastException

时间:2015-06-11 21:57:08

标签: java jboss jax-ws javamail classloader

尝试发送多部分消息时,我收到以下异常:

java.lang.ClassCastException: com.sun.xml.internal.ws.encoding.StringDataContentHandler cannot be cast to javax.activation.DataContentHandler
at javax.activation.MailcapCommandMap.getDataContentHandler(MailcapCommandMap.java:609)
at javax.activation.MailcapCommandMap.createDataContentHandler(MailcapCommandMap.java:563)
at javax.activation.DataHandler.getDataContentHandler(DataHandler.java:625)
at javax.activation.DataHandler.writeTo(DataHandler.java:329)
at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:268)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1366)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)
at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1345)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)
at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1345)
at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2106)
at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2074)
at com.ex.notificationservice.email.MailSender.send(MailSender.java:324)

MailSender.send方法的代码:

MimeMessage message = new MimeMessage(session);

message.setSubject(subject);

message.setFrom(new InternetAddress(fromAddr));

if (toAddrs != null) {
    for (String toAddr : toAddrs) {
        if (isEmpty(toAddr) == false) {
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddr));
        }
    }
} else {
    throw new MessagingException("MailSender: Destination address(es) not specified.");
}

Multipart multipart = new MimeMultipart("mixed");

MimeBodyPart mbp1 = new MimeBodyPart();

Multipart alternativeMultipart = new MimeMultipart("alternative");

MimeBodyPart textBodyPart = null;
MimeBodyPart htmlBodyPart = null;
MimeBodyPart xmlBodyPart = null;

if (isEmpty(textContent) == false) {
    textBodyPart = new MimeBodyPart();
    textBodyPart.setText(textContent);

    alternativeMultipart.addBodyPart(textBodyPart);
}

if (isEmpty(htmlContent) == false) {
    htmlBodyPart = new MimeBodyPart();
    htmlBodyPart.setContent(htmlContent, "text/html; charset=utf-8");

    alternativeMultipart.addBodyPart(htmlBodyPart);
}

if (isEmpty(xmlContent) == false) {
    xmlBodyPart = new MimeBodyPart();
    xmlBodyPart.setContent(xmlContent, "text/xml; charset=utf-8");

    alternativeMultipart.addBodyPart(xmlBodyPart);
}

mbp1.setContent(alternativeMultipart);

multipart.addBodyPart(mbp1);

if (files != null) {
    for (File file : files) {
        MimeBodyPart mimeBodyPart = new MimeBodyPart();

        mimeBodyPart.attachFile(file);

        multipart.addBodyPart(mimeBodyPart);
    }
}

message.setContent(multipart);

message.setSentDate(new Date());

//Exception occurs here
message.saveChanges();

Transport.send(message);

通常它在启动服务器后按预期工作并发送电子邮件一段时间。使用Jax-WS调用不相关的Web服务似乎可以解决问题。

我的理解是这个例外是典型的类加载器问题,但我的项目中没有包含异常中显示的包的任何库。

是否有可能另一个类加载器在启动时或运行时加载其中一个类?我看到JBoss有一个javax.activation.api模块,但是有太多其他模块依赖于它来使用jboss-deployment-structure.xml将其排除。

我正在使用JDK 1.7,JBoss 7.0.2,Java Mail 1.4.4。

1 个答案:

答案 0 :(得分:1)

JavaMail和JAX-WS之间的交互中有bug。最新版本的JDK包含针对此错误的修复程序。升级到JDK 1.8或最新版本的JDK 1.7。

解决方法可能是在初始化JAX-WS后执行此操作:

    MailcapCommandMap mailMap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
    mailMap.addMailcap("multipart/mixed;;x-java-content-handler=com.sun.mail.handlers.multipart_mixed");

如果需要处理多部分消息,可能会导致JAX-WS失败。