LinkedResource from Stream not working for MapiMessage Conversion

We are trying to create a LinkedResource using MailMessage and then converting it to MapiMessage. The problem is that MapiMessage doesn’t seem to be able to handle a LinkedResource created from a Stream. Here is an example to recreate the issue.


// Create mail message
MailMessage message = new MailMessage();
message.Sender = new MailAddress("Sender@doamain.com", “Sender Information”);
message.Subject = “This message is generated by Aspose”;
message.To = new MailAddress("to@doamian.com", " Recepient one");
message.CC = new MailAddress("to@doamian.com", " Recepient Two");

// Set HTML body of message. Use image source
message.HtmlBody = “This text will appear bold” +"<br/?
Here is embeded image ";

using (MemoryStream ms = new MemoryStream())
{
var bitmap = (Bitmap)Bitmap.FromFile(@“c:\mypath\myfile.png”); // base graphic
Graphics g = Graphics.FromImage(bitmap);
g.DrawString(“Dynamic Embedded Image”, new Font(“Tahoma”, 8, FontStyle.Bold), Brushes.White, new PointF(0, 0));
bitmap.Save(ms, ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);

// Initialize linked resource for image
LinkedResource image = new LinkedResource(ms);
image.ContentId = “logo”;

// Add image to linked resoorce
message.LinkedResources.Add(image);
}

//Create MapiMessage from MailMessage
MapiMessage mapiMsg = MapiMessage.FromMailMessage(message); // Dies here because of LinkedResource stream

//Save MailMessage
message.Save(“output.msg”, MailMessageSaveType.OutlookMessageFormat);

//Save MapiMessage
mapiMsg.Save(“outputMapi.msg”);

The error we receive is:
Unhandled Exception: System.InvalidOperationException: One of the streams has already been used and can’t be reset to the origin.
at ?▬◄.☼↓.ResetStream()
at Aspose.Email.Mail.AttachmentBase.Save(Stream stream)
at Aspose.Email.Outlook.MapiMessage.↓▼(LinkedResource , OutlookMessageFormat , Int32 , Boolean )
at Aspose.Email.Outlook.MapiMessage.?▼(MailMessage , MapiMessage , OutlookMessageFormat )
at Aspose.Email.Outlook.MapiMessage.?:bangbang:?(MailMessage , OutlookMessageFormat )
at Aspose.Email.Outlook.MapiMessage.FromMailMessage(MailMessage message, OutlookMessageFormat format, Boolean preserveSignature)
at Aspose.Email.Outlook.MapiMessage.FromMailMessage(MailMessage message)

How can we leverage Streams for LinkedResources while also relying on MapiMessage conversion? There is a performance penalty for writing our dynamic images to disk - we’d much rather keep the images in memory.

Thanks!

The fix for this is to ensure the MemoryStream is not disposed of before you call MapiMessage.FromMailMessage. In this example - the MemoryStream has been disposed of and cannot be accessed later down in the pipeline.


// Create mail message
MailMessage message = new MailMessage();
message.Sender = new MailAddress("Sender@doamain.com", “Sender Information”);
message.Subject = “This message is generated by Aspose”;
message.To = new MailAddress("to@doamian.com", " Recepient one");
message.CC = new MailAddress("to@doamian.com", " Recepient Two");

// Set HTML body of message. Use image source
message.HtmlBody = “This text will appear bold” +"<br/?
Here is embeded image ";

using (MemoryStream ms = new MemoryStream())
{
var bitmap = (Bitmap)Bitmap.FromFile(@“c:\mypath\myfile.png”); // base graphic
Graphics g = Graphics.FromImage(bitmap);
g.DrawString(“Dynamic Embedded Image”, new Font(“Tahoma”, 8, FontStyle.Bold), Brushes.White, new PointF(0, 0));
bitmap.Save(ms, ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);

// Initialize linked resource for image
LinkedResource image = new LinkedResource(ms);
image.ContentId = “logo”;

// Add image to linked resoorce
message.LinkedResources.Add(image);

//Create MapiMessage from MailMessage
MapiMessage mapiMsg = MapiMessage.FromMailMessage(message); // Dies here because of LinkedResource stream

//Save MailMessage
message.Save(“output.msg”, MailMessageSaveType.OutlookMessageFormat);

//Save MapiMessage
mapiMsg.Save(“outputMapi.msg”);
}

Hi MSG,


Thank you for using Aspose.Email.

You are right. The issue was with the usage of streams as in your earlier code, the “using” statement was not providing the space for the image. The solution is as you have described in your second code sample or initializing a new Stream object without “using”. In either case, the issue is fixed. Please feel free to write to us in case you have any other query/inquiry related to Aspose.Email. We’ll be glad to assist you further.