email - Attach a file from MemoryStream to a MailMessage in C# -


I am writing a program to attach a file to email. Currently I am saving the file using FileStream in the disk, and then I

  System.Net.Mail.MailMessage.Attachments.Add (New System NET.Mail. Attachments ("File Name"));   

I do not want to store the file in the disk, I want to store the file in memory and send it to the attachment from the storage stream.

Here is sample code.

  System.IO.MemoryStream ms = new System.IO .MemoryStream (); System.IO.StreamWriter Author = New System. Io Serial (ms); Author. Write ("Hello your sample file"); Writer.flush (); Writer.Dispose (); System.Net.Mime.ContentType ct = New System.Net.Mime.ContentType (System.Net.Mime.MediaTypeNames.Text.Plain); System.Net.Mail.Attachment Attached = New System.Net.Mail.Attachment (MS, CT); Attach. Content Disposition. Filename = "myFile.txt"; // I think you know about sending an email address with an email attachment after sending an email address. Close ();   

1 edit

You specify other file types by System.Net.Mime.MimeTypeNames such as System.Net.Mime Depending on the mime type you need to specify the correct extension in the file extension, for example "myFile.pdf" . .

Comments