0

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("yyy@yyy.com"));
            message.setRecipient(RecipientType.BCC, new InternetAddress("xxx@xxx.com"));
        } 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: xxx@xxx.com

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.

Paul
  • 19,704
  • 14
  • 78
  • 96
Julien
  • 200
  • 1
  • 15

1 Answers1

0

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.

Paul
  • 19,704
  • 14
  • 78
  • 96
  • It doesn't works, I edited my question in order to add the RecipientType.TO line. – Julien Mar 23 '18 at 16:44
  • Have you tried sending the e-mail or are you just generating the e-mail itself? – Paul Mar 23 '18 at 16:52
  • 1
    The MIME content of the message looks correct, and if you use JavaMail to send it the Bcc header will not be included in the message but will be used for recipients. It sounds like the problem is in how Outlook imports the MIME message. You might need to ask Microsoft about that. – Bill Shannon Mar 23 '18 at 19:35
  • I tried to send the email directly in my code and the bcc receive the mail. So, it works in my code but not with outlook (when i import the generated .eml). I'm completly lost about that. – Julien Apr 05 '18 at 07:48
  • My guess it's working as designed and is something that's not required but allowed by the RFC. BCC would be meaningless if the blind recipients were present in the exported e-mail. The RFC discusses this iirc. – Paul Apr 05 '18 at 12:30