Free Support Forum -

Memoryproblem with very large attachments


We are using Aspose.Networks to process bounce mails. Here most of the time one attachment of a mail is the original mail. Because we need to parse the original mail (the header of this mail), we use following code:
MailMessage msg = MailMessage.Load(stream);
foreach (Aspose.Network.Mail.Attachment a in msg.Attachments)
MemoryStream memStream = new MemoryStream();
*) a.Save(memStream);
memStream.Seek(0, SeekOrigin.Begin);

MailMessage attachment = MailMessage.Load(memStream, MessageFormat.Eml);

I don´t know, if there is another way to ‘convert’ a Attachment-object to an MailMessage-object. Our way works quite well but for very large attachments. If there is a very large attachment included this *) line will allocates a huge amount of memory. The attached mail has approx. 14 MB, loading the mail using the above way results in approx. 300 MB memory usage, which seems very bloated and is not acceptable in a productive envirnoment (because we ‘just’ want to parse some emails). I hope the attached mail helps.

We would appreciate if you could provide a solution.
Thank you.


I am not sure why you want to convert an Attachment-object to an MailMessage-object. Could you give me more detail about your scenario?



Could you give me more detail about your scenario? and what you want to achive



As said we are parsing bounce mails -> delivery status notifications. Most of the time this kind of mails have two attachments.

1. a text file containing a smtp error code
2. the original mail

We have to parse this original mail because we want to get the mime headers of this original mail (there is a special custom header we need).


Dear Andre,

Thanks for your reply.

Could you send your code about it? I think we can provide a more efficient way to do that. So I need to look into your code and provide a demo for you.

Send to guangzhou$


Hello, Andre,

I think you can use Aspose.Network.Outlook.MapiMessage directly, instead of using MailMessage. Since your major task is to parse the Outlook message document.

Here is the sample code in C#:

MapiMessage msg = MapiMessage.FromFile(file); //read from the outlook message

foreach (MapiAttachment att in msg.Attachments) //check attachment
if (att.ObjectData != null && att.ObjectData.IsOutlookMessage) //for embeded message
MapiMessage attmsg = MapiMessage.FromProperties(att.ObjectData.Properties);

Therefore, you don't need to cast a big object so many times.



Thank you for this hint. Is it possible to compine Aspose.Network.Outlook with the Aspose.Network.Exchange API?

I have tried to load message from an exchange server using the above code. Unfortunately an exception is the result.

foreach (ExchangeMessageInfo info in allMessages)
MemoryStream ms = new MemoryStream();
client.SaveMessage(info.UniqueUri, ms);
ms.Seek(0, SeekOrigin.Begin);
MapiMessage msg1 = MapiMessage.FromStream(ms);

Exception: Can’t open storage on LockBytes
And this is clear because client.SaveMessage saves the message as eml. And MapiMessage doesn´t support this.

So we need a way to retrieve mails from an exchange server (pop3 is no option) and to parse the attachments as described above.


Sorry, it looks like some mistakes here. Do you mean your scenario is

1) Fetch messages from the Exchange server, (message format is Eml)

2) Extract the attachment out of the message

3) Parse the attached message using MailMessage?

Ok, I will do some investigation on it.


We are fetching (bounce) mails from an exchange. These mails contain two attachments, which one of them is a mail (the original mail, because bounce mails are delivery status notifications). We have to parse this attached mail, because we need the mime headers of this attached mail.


We have fixed this bug. Here is the hotfix, please check it out:

The MailMessage.LoadAsMime is a new function that can load a attachment instance if it is a Eml formatted message.

Now you can load the attachment directly, without loading it to a memorystream. Moreover, the memory usage is also optimized.

Let me know whether it works.



It looks very good, thank you for the good work. This solution is exactly what we desired.