The MailAttachment Class



The MailAttachment Class

To simplify e-mail attachments, the .NET library includes the MailAttachment class. This class specifies the content and format of any attachments that are included in the mail message.

There are two constructors that can create a new mail attachment:

MailAttachment(string filename);
MailAttachment(string filename, MailEncoding encodetype);

The first constructor format creates a new mail attachment using the file specified in the filename path and encodes it using the UUEncode encoding type. The second constructor format allows you to specify an alternative encoding type, such as the Base64 encoding type used for MIME messages.

The System.Web.Mail namespace includes the MailEncoding enumeration that defines the two separate encoding types:

MailEncoding.Base64 For base64 MIME encoding

MailEncoding.UUEncode For uuencode encoding

You can query the encoding and filename properties of the MailAttachment object using the Encoding and Filename properties:

MailAttachment newfile = new
  MailAttachment("c:\\testfile.bmp", MailEncoding.Base64);
string filename = newfile.Filename;
if (newfile.Encoding == MailEncoding.Base64)
  Console.WriteLine("{0} is a MIME encoded file", filename);

When the MailAttachment object is created, it can be included in a MailMessage object using the Attachments property:

MailMessage newmessage = new MailMessage();
MailAttachment newfile = new MailAttachment("c:\\testfile.bmp");
newmessage.Attachments.Add(newfile);
newmessage.From = "frank@myisp.net";
newmessage.To = "melanie@myisp.net;nicholas@myisp.net";
newmessage.Body = "Here's my picture!!";
SmtpMail.Send(newmessage);

The MailAttachTest.cs program in Listing 13.3 demonstrates how to include file attachments with your e-mail messages.

Listing 13.3: The MailAttachTest.cs program
Start example
using System;
using System.Web.Mail;
class MailAttachTest
{
  public static void Main()
  {
   MailAttachment myattach =
     new MailAttachment("c:\\temp\\MailAttachTest.exe",
      MailEncoding.Base64);
   MailMessage newmessage = new MailMessage();
   newmessage.From = "barbara@shadrach.ispnet1.net";
   newmessage.To = "rich@shadrach.ispnet1.net";
   newmessage.Subject = "A test mail attachment message";
   newmessage.Priority = MailPriority.High;
   newmessage.Headers.Add("Comments",
         "This message attempts to send a binary attachment");
   newmessage.Attachments.Add(myattach);
   newmessage.Body = "Here's a test file for you to try";
   try
   {
     SmtpMail.SmtpServer = "192.168.1.100";
     SmtpMail.Send(newmessage);
   } catch (System.Web.HttpException)
   {
     Console.WriteLine("This device cannot send Internet messages");
   }
  }
}
End example

The MailAttachTest program creates a MailAttachment object, using the Base64 encoding scheme. Next a normal MailMessage object is created, and the MailAttachment object is added as an attachment:

newmessage.Attachments.Add(myattach);

This example also uses a remote SMTP mail relay server to forward the message to the intended recipients.

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows