Convert from MapiMessage to javax.mail.MimeMessage

Is there a more efficient way of converting from MapiMessage to java.mail.MimeMessage without having to allocate so many byte arrays and have so many steps? Forgive me if this is a silly question, since I don't know the Aspose libs very well.

public MimeMessage convertEmailBlobAspose(PersonalStorage pst, MessageInfo email) throws ArchivaException {

try {

MapiMessage message = pst.extractMessage(email);

ByteArrayOutputStream bos = new ByteArrayOutputStream();

try {

message.save(bos);

} finally {

StreamUtil.closeWithLogging(bos);

}

ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());

MailMessage mailMessage = null;

try {

mailMessage = MailMessage.load(bis,MessageFormat.getMsg());

} finally {

StreamUtil.closeWithLogging(bos);

}

bis = null;

bos = new ByteArrayOutputStream();

try {

mailMessage.save(bos,MessageFormat.getEml());

} finally {

StreamUtil.closeWithLogging(bos);

}

try {

bis = new ByteArrayInputStream(bos.toByteArray());

Properties props = System.getProperties();

Session session = Session.getInstance(props, null);

return new MimeMessage(session,bis);

} finally {

StreamUtil.closeWithLogging(bis);

}

} catch (Exception e) {

throw new ArchivaException("failed to convert message:"+e.getMessage(),e,logger);

}

}

Hi Jamie,

Thank you for sharing your concern with us.

Well, you can save a couple of lines by using the MailMessageInterpretor for conversion of MapiMessage to MailMessage. Please have a look at the following code sample for your kind reference and let us know your feedback.

Java Code Sample:

Properties props = System.getProperties();
Session session = Session.getInstance(props, null);

MapiMessage mapi = MapiMessage.fromFile("Some Mapi Message.msg");
MailMessageInterpretor mi = MailMessageInterpretorFactory.getInstance().getIntepretor(mapi.getMessageClass());
MailMessage mailMsg = mi.interpret(mapi);

ByteArrayOutputStream bos = new ByteArrayOutputStream();
mailMsg.save(bos, MailMessageSaveType.getEmlFormat());

new MimeMessage(session,new ByteArrayInputStream(bos.toByteArray()));

Hi There


I’ll give that a try, thanks! One thing though… I don’t normally like to allocate byte arrays when processing emails, since some of them can be 100 MB or more. I suppose when converting between formats, it may be unavoidable.

A couple of things, I’ve noticed about the Aspose API"s I’d like to draw your attention to:

o The API’s do not necessarily follow Java conventions. For example, normally you use methods such as getInputStream() and write(OutputStream os) to read and write from streams. Presumably this is because the Aspose Java API’s are auto ported. Its easy to see that the API’s are not necessarily implemented by native Java developers, although I am very glad to have this functionality available.
o Not sure why you are passing around string for filename’s. This is not the convention in Java. Normally you pass around File objects.
o Lack of consistency in usage of the API’s. That being said, I realize there are significant challenges in maintaining the consistency of API’s when they implemented by different developer.

Cheers

Jamie

Hi Jamie,


Thank you for sharing your concerns.

Yes, you are right. Aspose.Email for Java is auto-ported from its equivalent .NET version and the methods convention are followed as it is.

Regarding passing string for file name, these methods accept string objects that contain respective message file name, which can also be provided by loading a file object and getting file name from it. This follows from the same reason as I stated above. However, I’ll discuss this with our development team for possible implementation.