Saturday, August 20, 2011

How to send attachment with email?


To attach a file with your mail, add it to the mail by MailMessage.Attachments.Add() method.  The simple way to add a file to mail is to specify the file name.
MailMessage mail = new MailMessage();
 mail.Attachments.Add(new Attachment("FileName.txt"));

You can also specify a MIME content type which requires System.IO and System.Net.Mime namespace in addition to System.Net.Mail. The following code sample demonstrates how to use a Stream as a file attachment and how to specify MIME type:
MailMessage mail = new MailMessage();
Stream sr= new FileStream(@"FileName.txt", FileMode.Open, FileAccess.Read);
Mail.Attachments.Add(new Attachment(sr,” FileName.txt”, MediaTypeNames.Application.Octet));

No comments:

Post a Comment