简体   繁体   中英

Sending multi-part email from Google App Engine using Spring's JavaMailSender fails

It works fine without the multi-part (modified from the example in Spring documentation ):

final MimeMessagePreparator preparator = new MimeMessagePreparator() {
    public void prepare(final MimeMessage mimeMessage) throws Exception {
        final MimeMessageHelper message = new MimeMessageHelper(
                mimeMessage);
        message.setTo(toAddress);
        message.setFrom(fromAddress);
        message.setSubject(subject);
        final String htmlText = FreeMarkerTemplateUtils
                .processTemplateIntoString(configuration
                        .getTemplate(htmlTemplate), model);
        message.setText(htmlText, true);
    }
};
mailSender.send(preparator);

But once I change it to:

final MimeMessagePreparator preparator = new MimeMessagePreparator() {
    public void prepare(final MimeMessage mimeMessage) throws Exception {
        final MimeMessageHelper message = new MimeMessageHelper(
                mimeMessage, true);
...
        message.setText(plainText, htmlText);
    }
};
mailSender.send(preparator);

I get:

Failed message 1:
javax.mail.MessagingException: Converting attachment data failed
    at com.google.appengine.api.mail.stdimpl.GMTransport.sendMessage(GMTransport.java:231)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:402)
...

Since the GMTransport is a proprietary Google class and no source is available, it's pretty difficult to figure out the problem (at least with my skills). Anyone have any ideas what to try next?

My bean config, for helping you to help me:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"
        p:username="${mail.username}" p:password="${mail.password}"
        p:protocol="gm" />

I ran into this recently and could not find a solution on any websites. The problem lies in the multipartMode parameter used by default by MimeMessageHelper constructors.

Change:

MimeMessageHelper h = new MimeMessageHelper(mimeMessage, true);

To:

MimeMessageHelper h = new MimeMessageHelper(
    mimeMessage,
    MimeMessageHelper.MULTIPART_MODE_RELATED);

The first form will default to MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED . The documentation states:

This is the default since Spring 1.2.1. This is arguably the most correct MIME structure, according to the MIME spec: It is known to work properly on Outlook, Outlook Express, Yahoo Mail, and Lotus Notes. Does not work properly on Mac Mail. If you target Mac Mail or experience issues with specific mails on Outlook, consider using MULTIPART_MODE_RELATED instead.

It should probably be updated to include that it does not work with AppEngine's Mail API. The replacement mode may be less correct but appears to be more compatible.

I haven't had any issues using the vanilla (javax.mail.*) JavaMail libraries. See here http://code.google.com/appengine/docs/java/mail/usingjavamail.html#Multi_Part_Messages

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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