Amazon SES自定义标题List-Unsubscribe不起作用

时间:2015-04-08 20:10:21

标签: java http-headers amazon-ses

我正在尝试在我发送的电子邮件中添加“List-Unsubscribe”标题(通过amazon ses)但是当我看到收到的电子邮件时,其中没有这样的标题。我需要它来减少垃圾邮件投诉的数量,并提高可传递性和声誉。

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

PS:使用其他提供商,如Mandrill或Sendgrid,它可以工作,但我真的需要它在亚马逊

1 个答案:

答案 0 :(得分:4)

所以...我找到了一个解决方法。 如果要在邮件中添加自定义标题,请始终使用RawMessage,而不是简单的。

这样的事情:

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