views:

2038

answers:

3

I have an excel document represented as a byte[] and I'm wanting to send it as an attachment in an email.

I'm having a bit of trouble constructing the attachment.

I can create an Attachment which has the following constructors:

(Stream contentStream, ContentType contentType)
(Stream contentStream, string name)
(Stream contentStream, string name, string mediaType)

My idea at the moment is to create a MemoryStream from the byte[] and pass it to the method which creates the attachment.

Unfortunately I can't see a way to obtain the intended filename and content type from the MemoryStream and I also can't see how to supply the correct content type. There are options for plain text, Pdf, Rtf etc but none that I can see that immediately jump out at me as the one I should use for an Excel document.

The closest I can find is MediaTypeNames.Application.Octet which states:

The Octet member designates that the attachment contains generic binary data.

However, even if this is the one to use, unless it can be passed as a property of the Stream then my method for sending emails will only be able to send a byte[] as an Excel document...

Is there perhaps some other sort of Stream I could use? Or will I have to create my own type of Stream that has the details I need.

Surely someone out there has done this thing before and surely Microsoft would have thought this through to this level....

Any help would be much appreciated.

Update: Please don't vote for any answers that use the constructors that take the filename as a string. I'm really needing help using the ones that take a Stream...I want to avoid having to write the file to disk, email it, and then immediately delete it. Since there is a method that allows me to do that I'd like to use that one if at all possible.

Solution Update

Conrad managed to find what I was looking for! Thanks heaps man!

I'll just document the suggested solution just in case something happens to the content at the supplied link.

Credit for this solution goes to www.systemnetmail.com

static void AttachmentFromStream()
{

//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");

//set the content
mail.Subject = "This is an email";
mail.Body = "this content is in the body";

//Get some binary data
byte[] data = GetData();

//save the data to a memory stream
MemoryStream ms = new MemoryStream(data);

//create the attachment from a stream. Be sure to name the data 
//with a file and 
//media type that is respective of the data
mail.Attachments.Add( new Attachment( ms, "example.txt", "text/plain" ));

SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);
}

In my case, it just means I'll have to change my method to take the filename and fileformat as strings. I'll try using the Octet one...but failing that I'll just pass in the official MIME type.

All things considered, this is a pretty obvious solution...but I do appreciate the help in solving it...and the good thing is this solution will be documented for future programmers who have the same problem.

Thanks again everyone for you help!

A: 

--------------- I guess im wrong, this is if you have a file you want to attach ---------------

it looks like there is an example of sending mail with an attachment here:

http://www.aspnettutorials.com/tutorials/email/email-attach-aspnet2-csharp.aspx

I hope this is what you're looking for.

John Boker
unfortunately not because the method used in that example is to pass a string of the filename...which means the file must be written to disk before sending it. I'm wanting to do it all in memory since I have no reason to write the file to disk, send it, and then delete the file..
mezoid
sorry to down vote your answer John but someone up voted it and it is not the correct answer. Please, no one else vote for this answer...I need to create the attachment using the constructors that take a Stream not a string.
mezoid
A: 

The name parameter in the Attachment constructor is the name that will be displayed for the attachment in the recipient's email.

Thus you can freely choose the name parameter (extension .xls preferred), and set the mediaType parameter to "application/vnd.ms-excel", which is the defined MIME type for excel files.

devio
+2  A: 

The attachment constructor does indeed have a constructor that does what you need. I'm assuming you're using the System.Net.MailMessage class from .NET Framework 2. If so read this link for some sample code of what you need

Conrad
Thanks! That's exactly what I was looking for. How did you manage to find that? I assume we both use the same Google for searching?
mezoid
Ha ha! Well, let's just say in the course of m job I spend a lot of my time looking for answers to problems!
Conrad