In summary I am trying to send HTML emails on a google app engine application using javax.mail. The structure that I want for the email is:
- Alternative
- Text
- Related
- HTML
- Image
However when I try to build and send an email using this structure it fails on google app engine. One issue in particular was that no matter what the structure was I was not able to embed a multipart inside of another multipart and send that email on google app engine.
I have tried building and sending emails using this structure outside of the google app engine application using a smtp server and it works fine. However, migrating that same code to my real application fails to work.
I should note that I am able to send emails on the google app engine that are plain text or only html but when I try to add together all these pieces to compose a full email it fails.
Message msg = new MimeMessage(session);
Multipart multiPart = new MimeMultipart("alternative");
Multipart relatedMp = new MimeMultipart("related");
BodyPart txtBdp = new MimeBodyPart();
txtBdp.setContent(msgBody, "text/plain");
multiPart.addBodyPart(txtBdp);
BodyPart htmlBdp = new MimeBodyPart();
htmlBdp.setContent(htmlBody, "text/html");
relatedMp.addBodyPart(htmlBdp);
DataSource dataSrc = new FileDataSource(new File(imageFileNm));
BodyPart imageBdp = new MimeBodyPart();
imageBdp.setDataHandler(new DataHandler(dataSrc));
imageBdp.setFileName("logo.png");
imageBdp.setHeader("Content-ID", "<image_logo>");
imageBdp.setDisposition(MimeBodyPart.INLINE);
relatedMp.addBodyPart(imageBdp);
BodyPart relatedBdp = new MimeBodyPart();
relatedBdp.setContent(relatedMp);
multiPart.addBodyPart(relatedBdp);
msg.setFrom(new InternetAddress(emailAddr));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(emailAddr, recipientNm));
msg.setSubject(subject);
msg.setContent(multiPart);
msg.saveChanges();
Transport.send(msg);
The error message I am getting is:
Attachment needs content and name com.google.appengine.api.mail.MailService$Attachment.(MailService.java:56) com.google.appengine.api.mail.stdimpl.GMTransport.convertAttachments(GMTransport.java:334) com.google.appengine.api.mail.stdimpl.GMTransport.convertAttachments(GMTransport.java:321) com.google.appengine.api.mail.stdimpl.GMTransport.convertAttachments(GMTransport.java:295) com.google.appengine.api.mail.stdimpl.GMTransport.sendMessage(GMTransport.java:262) javax.mail.Transport.send(Transport.java:95) javax.mail.Transport.send(Transport.java:48)
I'm not sure why this code fails when I run it on google app engine considering that I am using the correct name format for the email from which to send it, and that it works outside of google app engine.