c# - How do I add an attachment to an email using System.Net.Mail? -


i have excel document represented byte[] , i'm wanting send attachment in email.

i'm having bit of trouble constructing attachment.

i can create attachment has following constructors:

(stream contentstream, contenttype contenttype) (stream contentstream, string name) (stream contentstream, string name, string mediatype) 

my idea @ moment create memorystream byte[] , pass method creates attachment.

unfortunately can't see way obtain intended filename , content type memorystream , can't see how supply correct content type. there options plain text, pdf, rtf etc none can see jump out @ me 1 should use excel document.

the closest can find mediatypenames.application.octet states:

the octet member designates attachment contains generic binary data.

however, if 1 use, unless can passed property of stream method sending emails able send byte[] excel document...

is there perhaps other sort of stream use? or have create own type of stream has details need.

surely out there has done thing before , surely microsoft have thought through level....

any appreciated.

update: please don't vote answers use constructors take filename string. i'm needing using ones take stream...i want avoid having write file disk, email it, , delete it. since there method allows me i'd use 1 if @ possible.

solution update

conrad managed find looking for! heaps man!

i'll document suggested solution in case happens content @ supplied link.

credit solution goes www.systemnetmail.com

static void attachmentfromstream() {  //create mail message mailmessage mail = new mailmessage();  //set addresses mail.from = new mailaddress("me@mycompany.com"); mail.to.add("you@yourcompany.com");  //set content mail.subject = "this email"; mail.body = "this content in body";  //get binary data byte[] data = getdata();  //save data memory stream memorystream ms = new memorystream(data);  //create attachment stream. sure name data  //with file ,  //media type respective of data mail.attachments.add( new attachment( ms, "example.txt", "text/plain" ));  smtpclient smtp = new smtpclient("127.0.0.1"); smtp.send(mail); } 

in case, means i'll have change method take filename , fileformat strings. i'll try using octet one...but failing i'll pass in official mime type.

all things considered, pretty obvious solution...but appreciate in solving it...and thing solution documented future programmers have same problem.

thanks again help!

the attachment constructor indeed have constructor need. i'm assuming you're using system.net.mailmessage class .net framework 2. if read link sample code of need


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -