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


i writing program attach file email. saving file using filestream disk, , use

system.net.mail.mailmessage.attachments.add(     new system.net.mail.attachment("file name"));  

i not want store file in disk, want store file in memory , memory stream pass attachment.

here sample code.

system.io.memorystream ms = new system.io.memorystream(); system.io.streamwriter writer = new system.io.streamwriter(ms); writer.write("hello 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 attach = new system.net.mail.attachment(ms, ct); attach.contentdisposition.filename = "myfile.txt";  // guess know how send email attachment // after sending email ms.close(); 

edit 1

you can specify other file types system.net.mime.mimetypenames system.net.mime.mediatypenames.application.pdf

based on mime type need specify correct extension in filename instance "myfile.pdf"


Comments