发送带有播放的电子邮件时出现异常

时间:2011-12-29 07:07:05

标签: playframework

从playframework发送电子邮件时收到以下错误 邮件通知程序的示例代码

        setFrom("xyz@xyz.com");
        setSubject("Relax 500 Error on production machine");        
        addRecipient("xyz@xyz.com");        
        send("errors/error1",errorContent); 

如果我使用 error1.txt ,一切正常我可以发送电子邮件。 但是当我将模板更改为 error1.html 时,播放无法发送文本/ html消息,因为某些原因我无法弄明白。

以下是例外情况:

Caused by: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
        java.io.IOException: "text/html" DataContentHandler requires String obje
ct, was given object of type class javax.mail.internet.MimeMultipart
        at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:625)
        at javax.mail.Transport.send0(Transport.java:169)
        at javax.mail.Transport.send(Transport.java:98)
        at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
        ... 8 more
Caused by: java.io.IOException: "text/html" DataContentHandler requires String o
bject, was given object of type class javax.mail.internet.MimeMultipart
        at com.sun.mail.handlers.text_plain.writeTo(text_plain.java:121)
        at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:86
9)
        at javax.activation.DataHandler.writeTo(DataHandler.java:302)
        at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
        at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1683)
        at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:585)
        ... 11 more

1 个答案:

答案 0 :(得分:2)

您使用的是哪个SMTP服务器?

使用Lotus Notes邮件服务器时遇到了类似的问题 - 仅在这种情况下。其他邮件服务器都很好。

(有点烦人)解决方案是复制Play的Mailer#send(Object ... args)方法并更改

HtmlEmail htmlEmail = new HtmlEmail();
htmlEmail.setHtmlMsg(bodyHtml);
if (!StringUtils.isEmpty(bodyText)) {
    htmlEmail.setTextMsg(bodyText);
}
email = htmlEmail;

public static Future<Boolean> sendMail(Map<String, Object> args) {
    ...
    HtmlEmail htmlEmail = new HtmlEmail();
    htmlEmail.setHtmlMsg(bodyHtml);
    htmlEmail.setContent(bodyHtml, "text/html"); // !! add this line!!
    if (!StringUtils.isEmpty(bodyText)) {
        htmlEmail.setTextMsg(bodyText);
    }
    email = htmlEmail;
    ...
}

明确设置htmlEmail对象的内容类型解决了问题。这样做的副作用是我们必须传递一个在邮件发送方法中明确定义的参数映射,例如

public static void sendTestMail(String to,
                                String subject,
                                String name) {
    addRecipient(to);
    setSubject(subject);

    Map<String, Object> args = new HashMap<String, Object>();
    args.put("name", name);

    sendMail(args);
}

在自定义sendMail方法中,您还需要更改绑定参数的代码,

for (Object o : args) {
    List<String> names = LocalVariablesNamesTracer.getAllLocalVariableNames(o);
    for (String name : names) {
        templateHtmlBinding.put(name, o);
        templateTextBinding.put(name, o);
    }
}

for (String key : args.keySet()) {
    templateHtmlBinding.put(key,
                            args.get(key));
    templateTextBinding.put(key,
                            args.get(key));
}

非常烦人,而且非常冗长,但它确实有效。