Reputation: 9319
I have following code:
try{
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setSubject(emailSubjectTxt);
msg.setFrom(new InternetAddress(emailFromAddress));
msg.setRecipient(
Message.RecipientType.TO,
new InternetAddress("[email protected]"));
MimeMultipart mp = new MimeMultipart();
BodyPart part = new MimeBodyPart();
part.setContent(emailMsgTxt, "text/html");
mp.addBodyPart(part);
msg.setContent(mp);
MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName("SupportBySkill.pdf");
BufferedInputStream bis = new BufferedInputStream(
SendMail.class.getResourceAsStream("SupportBySkill.pdf"));
attachment.setContent(bis, "application/pdf");
mp.addBodyPart(attachment);
// Capture the raw message
ByteArrayOutputStream out = new ByteArrayOutputStream();
msg.writeTo(out);
RawMessage rm = new RawMessage();
rm.setData(ByteBuffer.wrap(out.toString().getBytes()));
ClientConfiguration cc = new ClientConfiguration();
cc.setHttpClientFactory(new HttpClientFactory() {
public HttpClient createHttpClient(ClientConfiguration config) {
return new DefaultHttpClient(new GAEConnectionManager(),
new BasicHttpParams());
}
});
// Set AWS access credentials
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(
new BasicAWSCredentials("XXXXXX",
"XXXXXX"), cc);
// Call Amazon SES to send the message
try {
client.sendRawEmail(new SendRawEmailRequest().withRawMessage(rm));
} catch (AmazonClientException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e){e.printStackTrace();
log.severe("Could not send email. with error" + e.getMessage());
}
However on google app engine this code fails with error: Could not send email. with errorno object DCH for MIME type application/pdf
Please advise what could be wrong. This error on debugging comes on following line
msg.writeTo(out);
Upvotes: 0
Views: 2117
Reputation: 830
Amazon SES has a limited list of allowed file types and attached file name suffix and MIME type must match. See http://docs.amazonwebservices.com/ses/latest/DeveloperGuide/MIMETypes.html for the list of allowed MIME types.
I remember I had a hard time making attachments work with SES - in some cases MIME type was lost along the way (not appearing in generated raw email body), possible due to some JavaMail bug.
Anyway, here's the snippet that works for me:
byte[] bytes = getMyFileBytes();
DataSource ds = new ByteArrayDataSource(bytes, getMyMimeType());
MimeBodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(new DataHandler(ds));
attachment.setHeader("Content-Type", getMyMimeType());
attachment.setFileName(getMyFilename());
multipart.addBodyPart(attachment);
Upvotes: 1