Friday, December 26, 2008

Add memory stream data as attachment in the email using C#

Sometimes we have data in a stream object and we want to send that data as an attachment with an email. We can have data either in basic stream object, file stream object, or memory stream object. Let say, for this example we use memory stream object and below lines are used to add this memory stream data as attachment with an email and send it using SMTP Client.


objMemoryStream.Position = 0;
objMailMessage.Attachments.Add(new System.Net.Mail.Attachment(objMemoryStream, "testattachment.txt"));

Second line reads the memory stream and attach it with the mail message object as a text file.