bbottema / simple-java-mail

Simple API, Complex Emails (Jakarta Mail smtp wrapper)

Home Page:http://www.simplejavamail.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Attachments of nested Outlook messages (being attachments themselves) missing

bbottema opened this issue · comments

Originally reported under #298:

When reading nested outlook messages back from the attachment datasource (as attachments on an Email instance are datasources), attachments said outlook message are missing.

The reason for this is that reading nested Outlook messages as attachment of another Outlook msg is only possible by converting it to a Java structure first (using outlook-message-parser). This is a one-way conversion. Since the nested outlook message-as-an-attachment should be represented as attachment under an Email instance, it should be serialized to a datasource. Nested attachments then of this nested Outlook message are datasources themselves and cannot be serialized (javax.activation.DataSource is a JDK interface).

To solve this problem, a more comprehensive serialization solution is needed. Kryo should solve the problem nicely.

Released in 6.5.3 (@atmcq).

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!
}