Amazon SES custom header List-Unsubscribe isn't working

前端 未结 1 1309
你的背包
你的背包 2021-01-03 16:38

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

相关标签:
1条回答
  • 2021-01-03 16:57

    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;
    }
    
    0 讨论(0)
提交回复
热议问题