Monthly Archives: March 2011

Sending mail with attachments

MIME attachments are represented by Attachment objects, and are managed by use of the MailMessage.Attachments property. The source of attachments can be a file, a MemoryStream, or any other type of .NET Framework Stream. As a result, the ways an attachment can be created is limitless. This topic demonstrates two ways of creating an attachment.

C#:

const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "mytestpassword";
const int port = 465;
const SecurityMode securityMode = SecurityMode.Implicit;
SmtpClient client = new SmtpClient();
try
{
   MailMessage mmMessage = new MailMessage();
   mmMessage.From.Add("from@thedomain.com");
   mmMessage.To.Add("name@domain.com");
   mmMessage.Subject = "Test Subject";
   mmMessage.BodyText = "Test Content";
   // Attach file to the message.
   mmMessage.Attachments.Add("myfile.dat");
   // Attach content data from a stream to the message.
   Stream stream = new FileStream("myfile.dat", FileMode.Open);
   // The stream will be automatically closed after adding to the attachment list.
   mmMessage.Attachments.Add(new Attachment(stream, "teststream"));
   Console.WriteLine("Connecting SMTP server: {0}:{1}...", serverName, port);
   // Connect to the server.
   client.Connect(serverName, port, securityMode);
   // Login to the server.
   Console.WriteLine("Logging in as {0}...", user);
   client.Authenticate(user, password);
   Console.WriteLine("Sending the message with attachment...");
   client.Send(mmMessage);
   Console.WriteLine("Message sent...");
   // Disconnect.
   Console.WriteLine("Disconnecting...");
   client.Disconnect();
}
catch (SmtpException smtpExc)
{
   MessageBox.Show(string.Format("An SMTP error occurred: {0}, ErrorStatus: {1}", smtpExc.Message, smtpExc.Status));
}
catch (Exception exc)
{
   MessageBox.Show(string.Format("An error occurred: {0}", exc.Message));
}

VB.NET:

Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "mytestpassword"
Const port As Integer = 465
Const securityMode As SecurityMode = securityMode.Implicit
Dim client As New SmtpClient()
Try
    Dim mmMessage As New MailMessage()
    mmMessage.From.Add("from@thedomain.com")
    mmMessage.To.Add("name@domain.com")
    mmMessage.Subject = "Test Subject"
    mmMessage.BodyText = "Test Content"
    ' Attach file to the message.
    mmMessage.Attachments.Add("myfile.dat")
    ' Attach content data from a stream to the message.
    Dim stream As Stream = New FileStream("myfile.dat", FileMode.Open)
    ' The stream will be automatically closed after adding to the attachment list.
    mmMessage.Attachments.Add(New Attachment(stream, "teststream"))
    Console.WriteLine("Connecting SMTP server: {0}:{1}...", serverName, port)
    ' Connect to the server.
    client.Connect(serverName, port, securityMode)
    ' Login to the server.
    Console.WriteLine("Logging in as {0}...", user)
    client.Authenticate(user, password)
    Console.WriteLine("Sending the message with attachment...")
    client.Send(mmMessage)
    Console.WriteLine("Message sent...")
    ' Disconnect.
    Console.WriteLine("Disconnecting...")
    client.Disconnect()
Catch smtpExc As SmtpException
    MessageBox.Show(String.Format("An SMTP error occurred: {0}, ErrorStatus: {1}", smtpExc.Message, smtpExc.Status))
Catch exc As Exception
    MessageBox.Show(String.Format("An error occurred: {0}", exc.Message))
End Try

Deleting a POP3 message

Pop3Client class provides two methods Delete for deleting messages and Undelete for undeleting messages.

You use the Delete method to mark a message as deleted. It won’t appear in subsequent message lists, but will actually only be removed from the mailbox after the session is disconnected using Disconnect method. You can recover messages that were marked as deleted by using the Undelete method.

The following steps will help you to delete a single message using the Delete method:

C#:

// POP3 server information.
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "mytestpassword";
const int port = 995;
const SecurityMode securityMode = SecurityMode.Implicit;
// Create a new instance of the Pop3Client class.
Pop3Client client = new Pop3Client();
// Connect to the server.
client.Connect(serverName, port, securityMode);
// Login to the server.
client.Authenticate(user, password);
// Delete a mail message with sequence number 1.
client.Delete(1);
// Close the connection.
client.Disconnect();

VB.NET:

' POP3 server information.
Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "mytestpassword"
Const port As Integer = 995
Const securityMode As SecurityMode = securityMode.Implicit
' Create a new instance of the Pop3Client class.
Dim client As New Pop3Client()
' Connect to the server.
client.Connect(serverName, port, securityMode)
' Login to the server.
client.Authenticate(user, password)
' Delete a mail message with sequence number 1.
client.Delete(1)
' Close the connection.
client.Disconnect()