Error trying to read an email by MapiMessage.fromStream

Hi, I downloaded the trial version and made some test with the library Apose.Network for Java to read an msg attached file in an email from Outlook. When I use the metod MapiMessage.fromFile, it works very good but if I use the MapiMessage.fromStream, it sends me this error:

java.lang.IllegalStateException: This is not a structured storage file.
at com.aspose.network.obfuscated.c.r.(Unknown Source)
at com.aspose.network.obfuscated.c.h.a(Unknown Source)
at com.aspose.network.obfuscated.c.h.(Unknown Source)
at com.aspose.network.outlook.MapiMessage.fromStream(Unknown Source)

I read the stream directly from Outlook and then I pass it to that metod fromStream. What this error means? And what could I do to fix it?

Thanks,

Hi,

Thank you for inquiry.

Could you please provide some more details and sample code for us to reproduce the issue?

I did the following to test the scenario and got no error:

String strMsg = “d:\data\aspose\temp\test.msg”;

// load from disk
MapiMessage msg = MapiMessage.fromFile(strMsg);
System.out.println("Subject: " + msg.getSubject());

// load from stream
MapiMessage msg = MapiMessage.fromStream(fstream);
System.out.println("Subject: " + msg.getSubject());

Hi, thank you for your response.

I try to save the msg attached file in an email in a web application. This application is running in a Linux Server. First I get the emails from the Inbox in Outlook using JavaMail Api. Then I validate if the email has an attachment and if the attachment is a MSG File. If it is, I try to save the msg attach file, by passing the stream. This is part of the code:

public void handlePart(Part part,int i) throws MessagingException, IOException, Exception {
String disposition = part.getDisposition();
String storedir = “/Shared/temp/”;

if (disposition == null&&part.getContentType().equals(“MESSAGE/RFC822”)){

log.debug(“The attach is MSG!.”);

MimeBodyPart mbp = (MimeBodyPart)part;

MapiMessage mmf = MapiMessage.fromStream(mbp.getInputStream());

String subject = mmf .getSubject();

for(int i=0;i<mmf.getAttachments().size();i++) {

MapiAttachment attach=(MapiAttachment)mmf .getAttachments().get(i);

//Save attachment to the disk
attach.save(storedir + subject+"_"+ i +".msg");
}

}

Thank you in advance.



Hi,

Could you please try saving the stream (MSG attachment) to disk, give name like test.msg and try to open it in MS Outlook to see if it is a valid MSG file? If it opens in Outlook correctly, please post the saved MSG here, we will check it at our end.

Hi, I saved the stream in a msg file and I can open it in Outlook, but it put this message in the mail:

This mail has an msg file as attachment.

I tried to get the attachment using MapiMessage.fromStream using this msg file and it still send me the error message:

java.lang.IllegalStateException: This is not a structured storage file.
at com.aspose.network.obfuscated.c.r.(Unknown Source)
at com.aspose.network.obfuscated.c.h.a(Unknown Source)
at com.aspose.network.obfuscated.c.h.(Unknown Source)
at com.aspose.network.outlook.MapiMessage.fromStream(Unknown Source)

Thank you in advance.

Hi,

Could you please post this saved MSG file here for our reference?

Hi

I attach the msg file that I saved from a Stream. It has an msg attach file.

Thank you in advance.

Hi,

Thanks for posting the sample file.

The MSG file seems to be corrupt, that’s why it is not loaded in MapiMessage class using either fromStream() or fromFile() method.

Could you please tell how “<<Error: data corruption>>” message is inserted in the message body?

Also, does it happen with every message or only those messages with embedded MSG attachment?

Hi,

I saved the msg file again and it does not present the message <>.

Also I saved another msg file with another attachment and I made the same test and also still sending the error message:

java.lang.IllegalStateException: This is not a structured storage file.
at com.aspose.network.obfuscated.c.r.(Unknown Source)
at com.aspose.network.obfuscated.c.h.a(Unknown Source)
at com.aspose.network.obfuscated.c.h.(Unknown Source)
at com.aspose.network.outlook.MapiMessage.fromStream(Unknown Source)

But this error only appears when I use fromStream(), if I use fromDisk() it doesn't send any error.

I attach the new file that I saved.

Thanks.

Hi,

The MSG file attached in the above post is not corrupt. “Test.msg” loads fine with fromStream() method. I also tried to save the embedded message “pdf.msg” and tried loading it using fromStream(), it loaded correctly without any error message.

String strMsg = “d:\data\aspose\temp\pdf.msg”;
FileInputStream fstream = new FileInputStream(strMsg);
MapiMessage msg = MapiMessage.fromStream(fstream);

The stream in the above code comes originally from the disk, but technically it makes no difference if the stream comes from any other source.

"if (disposition == null&&part.getContentType().equals(“MESSAGE/RFC822”))"
One more thing from the above code, RFC 822 is an open standard for internet email. This format is not MSG and it cannot be loaded in the Aspose.Network for Java’s MapiMessage class.

However, our Aspose.Network for .NET product is capable of loading the RFC 822 (EML) files. We do have plans to provide such features in our Java product, so this would be possible in future when we provide support for loading EML message format files.