title: How do I add an attachment to an email using System.Net.Mail?
body: <p>I have an excel document represented as a byte[] and I'm wanting to send it as an attachment in an email.</p>
  
  <p>I'm having a bit of trouble constructing the attachment.</p>
  
  <p>I can create an Attachment which has the following constructors:</p>
  
  <pre><code>(Stream contentStream, ContentType contentType)
  (Stream contentStream, string name)
  (Stream contentStream, string name, string mediaType)
  </code></pre>
  
  <p>My idea at the moment is to create a MemoryStream from the byte[] and pass it to the method which creates the attachment.</p>
  
  <p>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. </p>
  
  <p>The closest I can find is <a href="http://msdn.microsoft.com/en-us/library/system.net.mime.mediatypenames.application.octet.aspx" rel="noreferrer">MediaTypeNames.Application.Octet</a> which states: </p>
  
  <blockquote>
    <p>The Octet member designates that the
    attachment contains generic binary
    data.</p>
  </blockquote>
  
  <p>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...</p>
  
  <p>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.</p>
  
  <p>Surely someone out there has done this thing before and surely Microsoft would have thought this through to this level....</p>
  
  <p>Any help would be much appreciated.</p>
  
  <p><strong>Update:</strong>
  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.</p>
  
  <p><strong>Solution Update</strong></p>
  
  <p>Conrad managed to find what I was looking for! Thanks heaps man!</p>
  
  <p>I'll just document the suggested solution just in case something happens to the content at the supplied link.</p>
  
  <p>Credit for this solution goes to <a href="http://www.systemnetmail.com/faq/3.4.2.aspx" rel="noreferrer">www.systemnetmail.com</a></p>
  
  <pre><code>static void AttachmentFromStream()
  {
  
  //create the mail message
  MailMessage mail = new MailMessage();
  
  //set the addresses
  mail.From = new MailAddress("me@mycompany.com");
  mail.To.Add("you@yourcompany.com");
  
  //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);
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Thanks again everyone for you help!</p>