0
votes

I want to make an email and extract the content in a .eml file. I don't understand why the following code doesn't works (at least, not like i want) :

public String getEML() {
        final Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol", "smtp");
        final Session session = Session.getInstance(properties);

        final MimeMessage message = new MimeMessage(session);
        final MimeMultipart mimeMultipart = new MimeMultipart("related");
        try {
            mimeMultipart.addBodyPart(getHtml());
            for(MimeBodyPart bodyPart : getImgs()) {
                mimeMultipart.addBodyPart(bodyPart);
            }

            //I don't want to send my mail in my code. I would like to extract an eml before.
            message.addHeader("X-Unsent", "1");
            message.setContent(mimeMultipart);
            message.setSubject("the subject");
            message.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
            message.setRecipient(RecipientType.BCC, new InternetAddress("[email protected]"));
        } catch (final MessagingException e) {
            e.printStackTrace();
        }

        // This function return the content of the mail in a String
        return messageMimeToString(message);
}

The .eml generated correspond to that i want. But, when I import this file in Outlook 2016, the BCC (Cci) is not set. However, the first line of the .eml is :

Bcc: [email protected]

Could someone explain this behaviour to me ?

EDIT: Just for more precision, when I replace RecipientType.BCC by RecipientType.TO or RecipientType.CC, it works.

1

1 Answers

0
votes

You need to set to as well. According to this answer there will be no RCPT command without a to, and RFC 5321 says the server may return an error if RCPT was not sent.

Edit: I'm no SMTP expert but RFC 5321 (Appendix B) says BCC addresses should not appear in the e-mail header:

Any BCC header fields SHOULD then be removed from the header section.