<<Back To Faq 2.3


Complete FAQ Listing


2.3 Attachment Class Printer Friendly


Click here for MSDN docs.

The Attachment class is used for creating and managing individual attachments of the MailMessage object. Attachments can be created from streams or file paths. The stream or file path must be set in the ctor of the Attachment.

Below is an example demonstrating the Attachment class

 

[ C# ]
 static void AttachmentFromFile()
{
//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";

//add an attachment from the filesystem
mail.Attachments.Add(new Attachment("c:\\temp\\example.txt"));

//to add additional attachments, simply call .Add(...) again
mail.Attachments.Add(new Attachment("c:\\temp\\example2.txt"));
mail.Attachments.Add(new Attachment("c:\\temp\\example3.txt"));

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);

}



[ VB.NET ]
 Sub AttachmentFromFile()
'create the mail message
Dim mail As 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"

'add an attachment from the filesystem
mail.Attachments.Add(New Attachment("c:\temp\example.txt"))

'to add additional attachments, simply call .Add(...) again
mail.Attachments.Add(New Attachment("c:\temp\example2.txt"))
mail.Attachments.Add(New Attachment("c:\temp\example3.txt"))

'send the message
Dim smtp As New SmtpClient("127.0.0.1")
smtp.Send(mail)
End Sub 'AttachmentFromFile



 



The formatted version of this faq can be found at http://www.SystemNetMail.com/faq/2.3.aspx