Attach Files to mail using C#
To attach a file, add it to the MailMessage.Attachments AttachmentCollection by calling the MailMessage.Attachments.Add method. The simplest way to add a file is to specify the file name
' VB Dim m As MailMessage = New MailMessage() m.Attachments.Add(New Attachment("C:\boot.ini"))
// C# MailMessage m = new MailMessage(); m.Attachments.Add(new Attachment(@"C:\boot.ini"));
You can also specify a MIME (Multipurpose Internet Mail Extensions) content type using the System.Net.Mime.MediaTypeNames enumeration. There are special MIME types for text and images, but you will typically specify MediaTypeNames.Application.Octet. The following code sample (which requires System.IO and System.Net.Mime in addition to System.Net.Mail) demonstrates how to use a Stream as a file attachment and how to specify the MIME type:
' VB Dim m As MailMessage = New MailMessage() Dim sr As Stream = New FileStream("C:\Boot.ini", FileMode.Open, FileAccess.Read) m.Attachments.Add(New Attachment(sr, "myfile.txt", MediaTypeNames.Application.Octet))
// C# MailMessage m = new MailMessage(); Stream sr = new FileStream(@"C:\Boot.ini", FileMode.Open, FileAccess.Read); m.Attachments.Add(new Attachment(sr, "myfile.txt", MediaTypeNames.Application.Octet));
|
No responses found. Be the first to respond and make money from revenue sharing program.
|