MailBee. NET Objects Tutorials

Bounced messages: SMTP part

It is often more convenient to have some sort of central location for the bounced messages. All returned emails should arrive to a single mailbox which is meant for bounce e-mails. Let us name our bounce message box - bounce@domain.com. Obviously, you need to be sure that the returned messages will find their way to the bounce box.

Let us see how these messages are routed by SMTP servers. When a message is submitted to an SMTP server it is tagged with a return-path. The return-path is the path that the server should use to communicate with the original sender of the message, and therefore the return-path is typically the e-mail address of the sender (the "From" address).

The return-path is recorded in the message. "From" header is used to display the sender address. The return-path and the address in the field "From" need not to be the same. Hence, you can send an email which will use field "From" to display the sender (jdoe@domain.com) but the return-path will be the bounce box address (bounce@domain.com). With this trick all bounce messages will be returned to a special mail box.

Let us see how you can do it using MailBee objects. Smtp.Send(senderEmail, recipients) method sends an email message to the specified recipients' e-mail addresses. The first parameter is the e-mail address of the sender. If it is a null reference, the e-mail address is taken from the "From" property. The second parameter contains comma-separated list of the recipients' e-mail addresses. The recipients list can be combined from To, Cc, and Bcc lists when the second parameter is empty. As you can see, the first parameter is all that we need to organize a bounce box. If you set the bounce box address to the first parameter, all the bounce mail will be delivered to the special address.

Note: Some SMTP servers do not allow sending e-mails from the address different from specified in "From" header.

Code example:

This sample sends an email using SMTP protocol. To make sure all the recipients will receive the message, TestSend is called. Messages with the delivery errors would come to the specified e-mail address.

C#

// Create SMTP object
Smtp mailer = new Smtp();

// Set the message fields.
mailer.From.AsString = "jdoe@domain.com";
mailer.To.AsString = "bill@domain2.com";
mailer.Subject = "Hi";
mailer.BodyPlainText = "This is test message";

// Starts logging SMTP activities into a file. 
mailer.Log.Enabled = true;
mailer.Log.Filename = @"C:\log.txt";
mailer.Log.Clear();

// Specify the server to use. If your server does not require authentication, 
// just omit both last parameters.
mailer.SmtpServers.Add("mail.domain.com", "jdoe", "secret");

// Attempt to connect.
mailer.Connect();

// Display the host name of the server the connection was established with.
Console.WriteLine("Connected to " + mailer.SmtpServers[mailer.GetCurrentSmtpServerIndex()].Name);

// Make sure all the recipients are ok.
if (mailer.TestSend(SendFailureThreshold.AllRecipientsFailed) != TestSendResult.OK)
{
        Console.WriteLine("No recipients can receive the message.");
}// Show refused recipients if any
else if (mailer.GetRefusedRecipients().Count > 0)
{
    Console.WriteLine("The following recipients failed: " + mailer.GetRefusedRecipients().ToString());
}
else
{
    Console.WriteLine("All recipients are ok. Will send the message now.");

    // Send e-mail. If it cannot be delivered, bounce will
    // arrive to bounce@domain3.com, not to joe@domain1.com
    mailer.Send("bounce@domain.com", (string)null);
    Console.WriteLine("Sent to: " + mailer.GetAcceptedRecipients().ToString());
}

// Disconnect from the server
mailer.Disconnect();

VB.NET

' Create SMTP object
Dim mailer As New Smtp

' Set the message fields.
mailer.From.AsString = "jdoe@domain.com"
mailer.To.AsString = "bill@domain2.com"
mailer.Subject = "Hi"
mailer.BodyPlainText = "This is test message"

' Starts logging SMTP activities into a file. 
mailer.Log.Enabled = True
mailer.Log.Filename = "C:\log.txt"
mailer.Log.Clear()

' Specify the server to use. If your server does not require authentication, 
' just remove last 2 parameters.
mailer.SmtpServers.Add("mail.domain.com", "jdoe", "secret")

' Attempt to connect.
mailer.Connect()

' Display the host name of the server the connection was established with.
Console.WriteLine("Connected to " + mailer.SmtpServers(mailer.GetCurrentSmtpServerIndex()).Name)

' Make sure all the recipients are ok.
If mailer.TestSend(SendFailureThreshold.AllRecipientsFailed) <> TestSendResult.OK Then
        Console.WriteLine("No recipients can receive the message.")
Else
    ' Show refused recipients if any
    If mailer.GetRefusedRecipients().Count > 0 Then
        Console.WriteLine("The following recipients failed: " & mailer.GetRefusedRecipients().ToString())
    Else
        Console.WriteLine("All recipients are ok. Will send the message now.")

        ' Send e-mail. If it cannot be delivered, bounce will
        ' arrive to bounce@domain3.com, not to joe@domain1.com
        mailer.Send("bounce@domain.com", CType(Nothing, String))
        Console.WriteLine("Sent to: " + mailer.GetAcceptedRecipients().ToString())
    End If
End If

' Disconnect from the server
mailer.Disconnect()