Save as a Stream

Hi,

I want to save a mailitem or mapimessage to a stream.

See my code:

Dim mailMsg As MailMessage = New MailMessage
mailMsg.From.Add(New MailAddress("from@company.com"))
mailMsg.To.Add(New MailAddress("to@company.com"))
mailMsg.Subject = “Subject”
mailMsg.HtmlBody = “Body”
Dim fStream As System.IO.Stream
mailMsg.Save(fStream, Aspose.Network.Mail.MessageFormat.Msg)

But I receive an error:

System.ArgumentNullException: Value cannot be null.
Parameter name: stream
at Aspose.Network.Outlook.MapiMessage.Save(Stream stream)
at Aspose.Network.Mail.MailMessage.Save(Stream stream, MessageFormat format)

I tried also with:

Dim outlookMsg As MapiMessage = MapiMessage.FromMailMessage(mailMsg)
outlookMsg.Save(fStream)

But I get the same error

Can you help me, please?

Hi,

Thanks for considering Aspose.

Could you please use MemoryStream class instead of Stream? It would work if you replace your Stream declaration line with the following one:

Dim fStream As New System.IO.MemoryStream

Hello,

You need to initialize the stream object before calling the save function, like:

fStream=File.OpenWrite("out.msg")//create a filestream for write

mailMsg.Save(fStream, Aspose.Network.Mail.MessageFormat.Msg) //save to it

Or perhaps,

you can just call the Save function by providing a filename.

Thanks