Repository navigation
Closed
Milestone
Description
Code:
public static void main(String[] args) throws IOException {
viewMsg();
viewEml();
}
public static void viewMsg() throws IOException {
Email em1 = EmailConverter.outlookMsgToEmail(new File("Email.msg"));
System.out.println("--------------------------------------------------------");
System.out.println(em1.getSubject());
System.out.println(em1.getAttachments());
System.out.println("--------------------------------------------------------");
}
public static void viewEml() throws IOException {
Email em1 = EmailConverter.emlToEmail(new File("Email.eml"));
System.out.println("--------------------------------------------------------");
System.out.println(em1.getSubject());
System.out.println(em1.getAttachments());
System.out.println("--------------------------------------------------------");
}Expected result:
Email
AttachmentResource
{
name='PHOTO-2020-02-18-07-42-08_1.jpg',
dataSource.name=PHOTO-2020-02-18-07-42-08_1.jpg,
dataSource.getContentType=image/jpeg
},
AttachmentResource{
name='This msg file is an attachment.msg',
dataSource.name=This msg file is an attachment.msg,
dataSource.getContentType=application/octet-stream
}
--------------------------------------------------------
--------------------------------------------------------
Email
AttachmentResource
{
name='PHOTO-2020-02-18-07-42-08_1.jpg',
dataSource.name=PHOTO-2020-02-18-07-42-08_1.jpg,
dataSource.getContentType=image/jpeg
},
AttachmentResource{
name='This msg file is an attachment.msg',
dataSource.name=This msg file is an attachment.msg,
dataSource.getContentType=application/octet-stream
}]
--------------------------------------------------------
Actual result:
Email
AttachmentResource
{
name='PHOTO-2020-02-18-07-42-08_1.jpg',
dataSource.name=PHOTO-2020-02-18-07-42-08_1.jpg,
dataSource.getContentType=image/jpeg
}
--------------------------------------------------------
--------------------------------------------------------
Email
AttachmentResource
{
name='PHOTO-2020-02-18-07-42-08_1.jpg',
dataSource.name=PHOTO-2020-02-18-07-42-08_1.jpg,
dataSource.getContentType=image/jpeg
},
AttachmentResource
{
name='This msg file is an attachment.msg',
dataSource.name=This msg file is an attachment.msg,
dataSource.getContentType=application/octet-stream
}
--------------------------------------------------------
Activity
bbottema commentedon Jan 6, 2021
Do you have a test message for me to analyse the problem with?
atmcq commentedon Jan 6, 2021
Sory yes, I meant to attach. Both Emails from the sample code are in the zip.
Email.zip
bbottema commentedon Jan 14, 2021
I tested this with the underlying library https://github.com/bbottema/outlook-message-parser directly which had no issues at all. I'll see what happens with Simple Java Mail...
bbottema commentedon Jan 14, 2021
Ahh, In OutlookEmailConverter#71, I can see it is omitted by design, because I didn't know how to translate an Outlook attachment to a Datasource (standard java type), which is the attachment data type in Simple Java Mail for all regular attachments (which already contains a byte[] and a mimetype, which is what comprises a Datasource).
I still wouldn't know how to convert that, because outlook-message-parser obviously returns a custom data class for nested Outlook msg file, rather than the msg file itself as raw binary file. Perhaps outlook-message-parser should include the original msg file in the custom class so we can always get the original data back...
Other ideas?
bbottema commentedon Feb 16, 2021
Ok, I've found a way to deal with this. So the problem is that Outlook parsing is one-way: from .msg to a Java object structure. Since these are attachments, they have to be DataSource objects instead. In this case I used Email's semi-recent ability to serialize to a binary value which can then be encapsulated as a DataSource.
So, here's how to to read an Email and access its nested Outlook message:
#298 parse nested outlook messages and convert to serialized Email Da…
bbottema commentedon Feb 16, 2021
Released in 6.5.0
atmcq commentedon Apr 29, 2021
Using 6.5.2 I am able to access a nested Outlook message as above, and I can use EmailConverter.emailToEML(nestedOutlookMessage) to get a .eml version of the nested message.
However - if the nested message itself includes an attachment (in my example a Word file), then I see the following error from emailToEML()
Exception in thread "main" java.lang.NullPointerException
at org.simplejavamail.converter.internal.mimemessage.MimeMessageHelper.determineResourceName(MimeMessageHelper.java:270)
at org.simplejavamail.converter.internal.mimemessage.MimeMessageHelper.getBodyPartFromDatasource(MimeMessageHelper.java:252)
at org.simplejavamail.converter.internal.mimemessage.MimeMessageHelper.setAttachments(MimeMessageHelper.java:193)
at org.simplejavamail.converter.internal.mimemessage.MimeMessageProducerMixedAlternative.populateMimeMessageMultipartStructure(MimeMessageProducerMixedAlternative.java:39)
at org.simplejavamail.converter.internal.mimemessage.MimeMessageProducer.populateMimeMessage(MimeMessageProducer.java:86)
at org.simplejavamail.converter.internal.mimemessage.MimeMessageProducerHelper.produceMimeMessage(MimeMessageProducerHelper.java:55)
at org.simplejavamail.converter.EmailConverter.emailToMimeMessage(EmailConverter.java:449)
at org.simplejavamail.converter.EmailConverter.emailToMimeMessage(EmailConverter.java:441)
at org.simplejavamail.converter.EmailConverter.emailToEML(EmailConverter.java:544)
Attached an email with attached Outlook message that works (Email.msg) and one that fails (Email2.msg).
Email2.zip
bbottema commentedon Apr 29, 2021
Thank you, I will look into it asap.
bbottema commentedon May 2, 2021
I was going to give up trying to serialize/deserialize the nested attachments, even after switching the Kryo. But I got it working finally.
So the OutlookModule now has an extra dependency on Kryo, but nested attachments are now maintained when trying the read back nested outlook messages. I'm including a convenient SerializationUtil to keep your code clean:
I haven't pushed the changes yet and haven't released yet, because my family came over now, but I'll get it out soon :)
atmcq commentedon May 4, 2021
Thanks for this.
#298 Added support for serializing attachments for nested Outlook mes…
bbottema commentedon May 4, 2021
Since this feaure was released already and can be in only one milestone, I've added a new issue #314 detailing this bug and its solution.
#298, #315: further improved support for nested Outlook messages, by …
bbottema commentedon Aug 7, 2026
Follow-up: the v9.2.0 work tracked in #690 preserves custom attachment content across native Java serialization by materializing it into a byte snapshot. The restored object deliberately is not the original custom
DataSource: lazy loading, network access, caching, write support, and custom methods do not come back.