Aspose.Network MailMessage and RTF Email - Cannot access closed stream error

Hi,

I am currently evaluating Aspose.Network for use in one of our Products and have come across an unexplained error that I cannot resolve.

I am trying to get out the various Body properties for use in a Email preview form.

I am having no problems pulling out the HTMLBody and Body properties for .MSG files that are either HTML or Text-only messages, but when I try to Load the message from a .MSG file and the .MSG file is an RTF message I get the error "Cannot access closed stream".

I am using Outlook 2007 and created the message by selecting Rich Text from the Format Group on the Options Tab of the Message Ribbon (as per the attached jpg), sending it to myself and then Saving As .MSG. I have attached the .MSG file in question to this post.

I have tried both of the following pieces of code to load the resulting .MSG file with the same result.

msg = MailMessage.Load(sMsgFileName, MessageFormat.Msg);

OR:

MapiMessageReader rdr = new MapiMessageReader(sMsgFileName);

mapimsg = rdr.ReadMessage();

The full stack trace is shown below.

Do you have any idea why this is happening and if there is a work around or fix in the pipeline.

Thanks!

Aaron.

" at System.IO.MemoryStream.get_Length()\r\n at Aspose.Network.Outlook.Storage.xd8c3135513b9115b.xe32d771fa0f8a73f(xe7be411416cfcd54 x630baaeb4d769680, xd686a7cfdb7bddb2 x5e4607c99a2c3e96)\r\n at Aspose.Network.Outlook.Storage.xd8c3135513b9115b.x0acd3c2012ea2ee8(Stream xcf18e5243f8d5fd3)\r\n at Aspose.Network.Outlook.x574bf3c3b35f401a.xf098323036d9ec26(x31c27b53bd5bfc49 xb6a159a84cb992d6, x525841b533d691b1 x72a03a41ff923a0b, xe7be411416cfcd54 x1aaf12d6975c8115)\r\n at Aspose.Network.Outlook.x0a2cab917e993500.Consume(xc1a5328245d170a8 message, x31c27b53bd5bfc49 parent, x525841b533d691b1 children, xe7be411416cfcd54 storageStream)\r\n at Aspose.Network.Outlook.xc2127bbe5f9a7dea.x8de50eb9cded41f8(xc1a5328245d170a8 x1f25abf5fb75e795, x31c27b53bd5bfc49 xda5bf54deb817e37, xe7be411416cfcd54 x1aaf12d6975c8115)\r\n at Aspose.Network.Outlook.xd58cfdd88485d179.Consume(xc1a5328245d170a8 message, x31c27b53bd5bfc49 parent, x525841b533d691b1 children, xe7be411416cfcd54 storageStream)\r\n at Aspose.Network.Outlook.xc2127bbe5f9a7dea.x8de50eb9cded41f8(xc1a5328245d170a8 x1f25abf5fb75e795, x31c27b53bd5bfc49 xda5bf54deb817e37, xe7be411416cfcd54 x1aaf12d6975c8115)\r\n at Aspose.Network.Outlook.MapiMessageReader.ReadMessage()\r\n at ShowEmail.Form1.button1_Click(Object sender, EventArgs e) in D:\\My Documents\\Visual Studio 2008\\Projects\\Preview Email\\ShowEmail\\Form1.cs:line 35\r\n at System.Windows.Forms.Control.OnClick(EventArgs e)\r\n at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)\r\n at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at System.Windows.Forms.ButtonBase.WndProc(Message& m)\r\n at System.Windows.Forms.Button.WndProc(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)\r\n at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)\r\n at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n at ShowEmail.Program.Main() in D:\\My Documents\\Visual Studio 2008\\Projects\\Preview Email\\ShowEmail\\Program.cs:line 17\r\n at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)\r\n at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading.ThreadHelper.ThreadStart()"

Hi Aaron,

Thanks for sending the msg file and details. I have tested and encountered the same problem. Our developers will look into it in more detail and give you the feedback soon.

I have sent the hotfix dll at your email address. Please check if it works for you.

Hello,

Please check the fix for this issue:

http://www.aspose.com/community/files/51/file-format-components/aspose.network/default.aspx

Thanks

Hi,

Thanks for your (very) prompt response and hotfix!

I have tested the latest release (4.6.0.0) with the RTF message attached to the initial post and it is working fine when I load the .msg file from a local file, e.g:

msg = MailMessage.Load(sMsgFileName, MessageFormat.Msg);

However, when loading the .msg file from a stream using the following code:

MemoryStream fileStream = new MemoryStream();

...

MailMessage msg = MailMessage.Load(fileStream,MessageFormat.Msg);

It is failing with the following error:

"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

Unfortunately this is the context in which I need to use the component.

Are you able to replicate this issue?

Thanks.

Hello,

I cannot reproduce this exception. I used code like this:

byte[] buf = new byte[1024];
using (FileStream fs = new FileStream(@"RichText.msg", FileMode.Open))
{
MemoryStream ms = new MemoryStream();
int off = 0;
int count = 0;
while ((count = fs.Read(buf,0, buf.Length)) >0)
{
ms.Write(buf, 0, count);
// off += count;
}

ms.Seek(0, SeekOrigin.Begin);
MailMessage msg = MailMessage.Load(ms, MessageFormat.Msg);
msg.Save("rich2.eml");
}

Could you please

1. Provide more details of your code so that we can repro the problem.

2. Provide me the msg file you are using

Thanks very much

Hi,

My code is almost identical to yours except that I am passing a Stream object to the Load method from a SharePoint SPFile object using SPFile.OpenBinaryStream:

MailMessage msg = MailMessage.Load(currentFile.OpenBinaryStream(), MessageFormat.Msg);

The file I attached to the orgininal email exhibits the behaviour as do ALL my .msg files regardless of their internal type.

I believe the issue only occurs when the Aspose.Network assembly is in the GAC. Our solution deploys the referenced assemblies to the GAC by default. It seems that if I put it in the GAC then the second time I run the console app (note not the second iteration of the code within the same application instance) then I get the error.

It seems to me like there is some stream or file pointed that is not being released when the assembly is in the GAC, which is very odd! If I then remove the Aspose.Network assembly from the GAC remove the reference in my project to Aspose.Network and then re-build and execute the problem goes away.

I can look to see if there is a way to NOT put the assembly in the GAC, but I don't want to have to do that unless there is no fix for this issue. I'm happy to provide any further details you might like, please let me know!

Thanks.

Hi,

Can anyone comment on this issue? I am still waiting for a fix or confirmation that one will be made before I confirm my decision to purchase.

Thanks.

Aaron.

Hello, Aaron,

Thanks for your considering Aspose.

I would like to confirm the environment that you have this problem.

1. what OS? windows 2003 or xp?

2. What version of .net framework?

3. Any other environment settings?

4. Can you use the file stream in your application as a workaround solution?

We cannot reproduce this problem on our local machine. The dll is added to GAC but it is working fine.

Thanks for your help.

Hi,

Could you please run the test application on your system and test whether this application throws the exception or not? This test application had a reference to the Aspose.Network.dll which is registered in the GAC and loads the RichText.msg file (using stream) that was originally posted by you.

If the test application works fine, then we will try to create a similar environment as yours and do some tests in that environment. In this case, please suggest which software we need to install e.g. OS, SharePoint Server, SDK, etc. If that information is confidential, you can send it to me via email ("Contact" --> "send saqib.razzaq and email").

Hi,

The test application works fine for me too.

I am running Windows Server 2008 x64, .Net Framework 2.0 and Microsoft Office SharePoint Server 2007 SP1.

The code I am using is:

string sPreviewText = string.Empty;

try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite sourceSite = new SPSite(sSourceWeb))
{
using (SPWeb sourceWeb = sourceSite.OpenWeb())
{
//Get the file.
SPFile currentFile = sourceWeb.GetFile(sSourceFile);

//Read the file into a Memory Stream.
MemoryStream fileStream=new MemoryStream();
byte[] currentFileContent = currentFile.OpenBinary();
fileStream.Write(currentFileContent, 0, currentFileContent.Length);
fileStream.Position = 0;

//Create a MAPI Message object from the file stream.
MapiMessage msg = MapiMessage.FromStream(fileStream);
fileStream.Close();
fileStream.Dispose();

//Return the body in RTF format.
sPreviewText = msg.BodyRtf.ToString();
}
}
});

return sPreviewText;

}
catch (Exception)
{
return string.Empty;
}

Thanks.

Aaron.