Skip to content

outlookMsgToEmail loses nested msg attachments #298

Closed

Description

@atmcq

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

  1. bbottema commented on Jan 6, 2021

    @bbottema
    Owner

    Do you have a test message for me to analyse the problem with?

  2. atmcq commented on Jan 6, 2021

    @atmcq
    Author

    Sory yes, I meant to attach. Both Emails from the sample code are in the zip.

    Email.zip

  3. bbottema commented on Jan 14, 2021

    @bbottema
    Owner

    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...

  4. bbottema commented on Jan 14, 2021

    @bbottema
    Owner

    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?

  5. bbottema commented on Feb 16, 2021

    @bbottema
    Owner

    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:

    public Email getNestedOutlookMessage(...yourOutlookMessageSource) {
    	Email email = EmailConverter.outlookMsgToEmail(yourOutlookMessageSource);
    
    	// now the nested Outlook message is converted to an Email and subsequently serialized:
    	// email.getAttachments().get(0).getName() == "attachment 0 as nested Outlook message (converted).sjm"
    
    	InputStream sjmInputstream = email.getAttachments().get(0).getDataSourceInputStream();
    	Email nestedOutlookMessage = deserializeEmail(sjmInputstream);
    	
    	return nestedOutlookMessage;
    }
    
    
    private Email deserializeEmail(InputStream sjmInputStream) {
    	try (ObjectInputStream in = new ObjectInputStream(sjmInputStream)) {
    		return (Email) in.readObject();
    	} catch (final ClassNotFoundException | IOException ex) {
    		throw new SerializationException(ex);
    	}
    }
  6. added this to the 6.4.6 milestone on Feb 16, 2021
  7. added a commit that references this issue on Feb 16, 2021
  8. bbottema commented on Feb 16, 2021

    @bbottema
    Owner

    Released in 6.5.0

  9. atmcq commented on Apr 29, 2021

    @atmcq
    Author

    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

  10. bbottema commented on Apr 29, 2021

    @bbottema
    Owner

    Thank you, I will look into it asap.

  11. bbottema commented on May 2, 2021

    @bbottema
    Owner

    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:

    public Email getNestedOutlookMessage(...yourOutlookMessageSource) {
    	Email email = EmailConverter.outlookMsgToEmail(yourOutlookMessageSource);
    
    	// now the nested Outlook message is converted to an Email and subsequently serialized:
    	// 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);
    	
    	return nestedOutlookMessage; // now with attachments, yay!
    }
    

    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 :)

  12. atmcq commented on May 4, 2021

    @atmcq
    Author

    Thanks for this.

  13. added a commit that references this issue on May 4, 2021
  14. modified the milestones: 6.5.0, 6.5.3 on May 4, 2021
  15. bbottema commented on May 4, 2021

    @bbottema
    Owner

    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.

  16. added a commit that references this issue on May 22, 2021
  17. bbottema commented on Aug 7, 2026

    @bbottema
    Owner

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions