I\'m tryin to add the \"List-Unsubscribe\" header in my sent emails (through amazon ses) but when i see the received email there\'s no such header in it. I need it to reduce
So... i found a workaround. If you want to add a custom header to your message, always use a RawMessage, not a simple one.
Something like this:
SendRawEmailRequest sendRawEmailRequest = new SendRawEmailRequest();
RawMessage rawMessage = null;
rawMessage = buildSimpleRawMessage(...);
sendRawEmailRequest.setRawMessage(rawMessage);
private RawMessage buildSimpleRawMessage(String subject, String message, Attachment attachment) {
RawMessage rawMessage = null;
try {
// JavaMail representation of the message
Session s = Session.getInstance(new Properties(), null);
MimeMessage mimeMessage = new MimeMessage(s);
// Subject
mimeMessage.setSubject(subject);
// Add a MIME part to the message
MimeMultipart mimeBodyPart = new MimeMultipart();
BodyPart part = new MimeBodyPart();
part.setContent(message, "text/html");
mimeBodyPart.addBodyPart(part);
// Add a attachement to the message
if(attachment!=null){
part = new MimeBodyPart();
DataSource source = null;
source = new ByteArrayDataSource(attachment.getBuf(), attachment.getMimeType());
part.setDataHandler(new DataHandler(source));
part.setFileName(attachment.getFileName());
mimeBodyPart.addBodyPart(part);
}
mimeMessage.setContent(mimeBodyPart);
mimeMessage.addHeader(UNSUBSCRIBE_HEADER, unsubscribeURL);
// Create Raw message
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
mimeMessage.writeTo(outputStream);
rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));
} catch (Exception e) {
logger.error("There was a problem creating mail attachment", e);
throw Throwables.propagate(e);
}
return rawMessage;
}