bbottema/simple-java-mail

Generating an EML from an Email object with nested attachments, when created from an Outlook MSG file

atmcq opened this issue · 6 comments

atmcq commented

Following #298 I can access nested Outlook attachments within Email objects that have been created by EmailConverter.outlookMsgToEmail().

However if I subsequently use EmailConverter.emailToEML() on the Email object created in this way, the nested Outlook attachment is replaced with the serialised object as an SJM file, e.g. "attachment 1 as nested Outlook message (converted).sjm".

It looks like it needs to be deserialised and converted back to a .MSG or .EML attachment as part of emailToEML().

public static void main(String[] args) throws IOException {
    viewMsg();	
}
	
public static void viewMsg() throws IOException {
    Email em1 = EmailConverter.outlookMsgToEmail(new File("Email2.msg"));
    String emlStr = EmailConverter.emailToEML(em1);
    FileOutputStream outputStream = new FileOutputStream("NewEml.eml");
    byte[] strToBytes = emlStr.getBytes();
    outputStream.write(strToBytes);
    outputStream.close();
}

Examples.zip

atmcq commented

It looks like it needs to be deserialised and converted back to a .MSG or .EML attachment as part of emailToEML().

My thinking here was that if I am using emailToEml() then the user may need to access the nested attachment in the same mail client that needs .eml and therefore converting the nested attachment to .eml makes sense.

So with the latest change, that .sjm file can be accessed as follows:

	// email.getAttachments().get(0).getName() == "attachment 0 as nested Outlook message (converted).sjm"
	InputStream sjmInputstream = email.getAttachments().get(0).getDataSourceInputStream();
	Email nestedOutlookMessage = SerializationUtil.deserialize(sjmInputstream);

However, I didn't realize I could just serialize to an EML string instead, which agree with you is the better solution, because this removes the need for Kryo to handle datasource serialization. Also the file is self-describing, as people would generally recognize .eml, but certainly not .sjm.

I'll perform the changes when I have some time. Thanks for thinking along with me on this.

Great, thanks! I also agree for EML, this makes sense. Waiting this :-)
In Outlook .msg attachment filename is taken from attachement .msg subject. Maybe the same can be done:
builder.withAttachment(email.getSubject() != null ? email.getSubject() + ".eml" : "attachment " + i + " as nested Outlook message (converted).eml", new ByteArrayDataSource(MiscUtil.serialize(EmailConverter.emailToMimeMessage(email).getInputStream()), "application/octet-stream"));
But it can produce errors or be dangerous if specials characters are present in subject...
(And not sur of this part of code, just for example: new ByteArrayDataSource(MiscUtil.serialize(EmailConverter.emailToMimeMessage(email).getInputStream()))

atmcq commented

For us this feels like more than an enhancement. We are a SAAS case management app. We allow users to email the system direct, or upload an email file onto a case. We want to import the mail, bring in all attachments from the mail into the documents area of the case and also store a version of the original mail for context. We've started to use SimpleJavaMail for this as it allows us to handle Outlook .msg files. Our use case is as follows -

  1. Email arrives in system, or user uploads .eml or .msg file
  2. Use SimpleJavaMail to generate Email object, depending on input type with mimeMessageToEmail(), emlToEmail() or outlookMsgToEmail()
  3. Process mail and loop through attachments as necessary an process these, including nested messages
  4. Save original email as EML on case using emailToEml()

We use emailToEml() to guarantee that customers don't need Outlook to view the original message, so it is important to us that this method handles the attachments in a way that people can recognise. We don't want to have to save in .msg format before processing as we then lose the universal access that .eml gives.

If there is any chance you could prioritise this we'd appreciate this - similarly if there is anything we can do to help I can try.

Thanks

It's good to be reminded every now and then that this library is actually being used in real life. I know from the download figures that this is the case of course, but without many but reports it's easy to forget.

I've released a fix for this in 6.5.4. Nested attachments are now stored as regular EML attachments and also I started using Outlook's own strategy for naming these nested messages, by simply using their subject as attachment/file name.

Cheers!

Thanks for this!