Amazon SES custom header List-Unsubscribe isn'

2019-02-11 07:05发布

问题:

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 the number of spam complaints and to improve deliverability and reputation.

SendEmailRequest sendEmailRequest = new SendEmailRequest();
sendEmailRequest.putCustomRequestHeader(UNSUBSCRIBE_HEADER, unsuscribeURL);

PS: Using others providers such as Mandrill or Sendgrid it works, but i really need it at amazon

回答1:

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;
}