Accepted and refused recipients


Introduction

If you send an e-mail to multiple recipients, it may turn out that the server has accepted some recipients while other recipients have been rejected. The SMTP protocol is very permissive in this regard, and the message will be accepted for delivery even if at least one recipient was accepted. So does MailBee.NET.

However, such "partial delivery" may not be suitable for you in case if you believe your e-mail makes sense only if delivered to all the recipients). In this case, you can tell MailBee.NET to cancel the submission of the e-mail to the SMTP relay server if any recipient gets rejected.

In case if you allow failed recipients, you can then track which recipients have succeeded and which ones have failed. You can do this both during and after sending the message. You can also make a decision whether to allow failed recipients on per-recipient basis.

Anyway, it's important to understand that even if your SMTP relay server has accepted some recipients, this does not yet guarantee they will actually receive the message. For performance sake, your server may just accept all the e-mail addresses you're submitting to it without making any additional checks. Also, even if the server finds the particular e-mail address to be valid, it does not yet mean that address actually exists (it only means the domain part of the address is valid).

Note that you can enable or disable failed recipients only when sending via an SMTP relay server. If you're using direct send via DNX MX lookup, it's not possible to disallow failed recipients. This is because direct send is not an "atomic" operation (transaction) which can be easily rolled back at any moment.

For instance, if you send to joe@firstdomain.com and jane@seconddomain.com via DNS MX lookup, MailBee.NET needs to create separate connections with SMTP MX servers of firstdomain.com (MX1) and seconddomain.com (MX2). If MX1 has accepted the message but MX2 hasn't, it's not possible to tell MX1 to "forget" the message it just received.

Of course, direct send does not permit failed recipients if you send to a single recipient only (if it fails, nobody will receive the message, so you never have the "partial delivery" problem with single recipients). Due to this and some other reasons, it's recommended to send e-mails in "direct send" mode only to single recipients.


Allow or disallow refused (failed) recipients

By default, refused (failed, rejected) recipients are allowed. To enable that the e-mail will be sent only if all the recipients are accepted, set SmtpServer.AllowRefusedRecipients to false in the SmtpServer object which represents the SMTP relay server you're connecting to:

Smtp mailer = new Smtp();

// Use SMTP relay server with authentication.
SmtpServer server = mailer.SmtpServers.Add("mail.here.com", "joe", "secret");

// Disallow rejected recipients.
server.AllowRefusedRecipients = false;
Dim mailer As New Smtp()

' Use SMTP relay server with authentication.
Dim server As SmtpServer = mailer.SmtpServers.Add("mail.here.com", "joe", "secret")

' Disallow rejected recipients.
server.AllowRefusedRecipients = False

The samples throughout this guide assume that MailBee and MailBee.SmtpMail namespaces are imported and MailBee.NET SMTP license key is set. See Import namespaces and set license key topic for details.


Get successful and failed recipients after sending e-mail

Once the message has been sent, you can check which recipients have been accepted or rejected. The sample below displays failed and successful recipients in two different ways:

Smtp mailer = new Smtp();

// Use SMTP relay server with authentication.
SmtpServer server = mailer.SmtpServers.Add("mail.example.com", "jane", "secret");

// Allow rejected recipients (true is default value so you can remove this line).
server.AllowRefusedRecipients = true;

// The message is not very important. If someone can't receive it, no big deal.
mailer.Message.From.Email = "jane@example.com";
mailer.Message.Subject = "Notification";
mailer.Message.BodyPlainText = "I'm going on vacation";

// We'll send the message to 3 addresses.
mailer.Message.To.Add("bill@domain1.com", "Bill C.");
mailer.Message.To.Add("mike@domain2.com", "Mike T.");
mailer.Message.To.Add("john@domain3.com", "John D.");

// Will succeed if at least one recipient is accepted.
mailer.Send();

// Report e-mail addresses (with display names) of accepted and rejected recipients.
Console.WriteLine("Succeessful: " + mailer.GetAcceptedRecipients().ToString());
Console.WriteLine("Failed: " + mailer.GetRefusedRecipients().ToString());

Console.WriteLine();

// Report e-mail addresses (no display names) of accepted and rejected recipients.
foreach (EmailAddress addr in mailer.GetAcceptedRecipients())
{
    Console.WriteLine("Successful e-mail: " + addr.Email);
}
foreach (EmailAddress addr in mailer.GetRefusedRecipients())
{
    Console.WriteLine("Failed e-mail: " + addr.Email);
}
Dim mailer As New Smtp()

' Use SMTP relay server with authentication.
Dim server As SmtpServer = mailer.SmtpServers.Add("mail.example.com", "jane", "secret")

' Allow rejected recipients (true is default value so you can remove this line).
server.AllowRefusedRecipients = True

' The message is not very important. If someone can't receive it, no big deal.
mailer.Message.From.Email = "jane@example.com"
mailer.Message.Subject = "Notification"
mailer.Message.BodyPlainText = "I'm going on vacation"

' We'll send the message to 3 addresses.
mailer.Message.To.Add("bill@domain1.com", "Bill C.")
mailer.Message.To.Add("mike@domain2.com", "Mike T.")
mailer.Message.To.Add("john@domain3.com", "John D.")

' Will succeed if at least one recipient is accepted.
mailer.Send()

' Report e-mail addresses (with display names) of accepted and rejected recipients.
Console.WriteLine("Succeessful: " & mailer.GetAcceptedRecipients().ToString())
Console.WriteLine("Failed: " & mailer.GetRefusedRecipients().ToString())

Console.WriteLine()

' Report e-mail addresses (no display names) of accepted and rejected recipients.
Dim addr As EmailAddress
For Each addr In mailer.GetAcceptedRecipients()
   Console.WriteLine("Successful e-mail: " & addr.Email)
Next addr
For Each addr In mailer.GetRefusedRecipients()
   Console.WriteLine("Failed e-mail: " & addr.Email)
Next addr

In case if your server always accepts all recipients regardless if they belong to existing domains or not, you can check the work of the sample by editing one or two recipients' e-mail address to make them invalid (e.g. remove @ symbol). The server, even if it does not check the validity of domain part, should at least perform syntax check on every address.


Use events to track successful and failed recipients during sending e-mail

Events allow your application to find out which recipients have been accepted or rejected before the completion of the sending process. You may even cancel the sending if you find out that an important recipient was rejected.

The console sample below sends an e-mail to a primary recipient (listed in To) and two secondary recipients (listed in CC). Let's assume we only care of primary recipients. If CC recipients receive the message, that's great. If they won't, no big deal. But we'll cancel the sending process in case if anyone listed in To field gets rejected:

using System;
using MailBee;
using MailBee.Mime;
using MailBee.SmtpMail;

class Sample
{
    private static void OnRecipient(object sender,
        SmtpMessageRecipientSubmittedEventArgs e)
    {
        // If the recipient was accepted, just move on.
        if (e.Result)
        {
            return;
        }

        // Check if it was the To recipient who was rejected.
        foreach (EmailAddress addr in e.MailMessage.To)
        {
            if (addr.Email == e.RecipientEmail)
            {
                e.AllowRefusedRecipient = false;
            }
        }
    }

    static void Main(string[] args)
    {
        Smtp mailer = new Smtp();

        // Use SMTP relay server with authentication.
        SmtpServer server = mailer.SmtpServers.Add(
            "mail.example.com", "jane", "secret");

        // Allow rejected recipients (the default state).
        server.AllowRefusedRecipients = true;

        // The message is not important to anyone but the boss.
        mailer.Message.From.Email = "jane@example.com";
        mailer.Message.Subject = "Notification";
        mailer.Message.BodyPlainText = "I'm going on vacation";

        // We'll send the message to 1 important and 2 other addresses.
        mailer.Message.To.Add("boss@example.com", "Big Boss");
        mailer.Message.Cc.Add("mike@here.com", "Mike T.");
        mailer.Message.Cc.Add("john@there.com", "John D.");

        // Subscribe to MessageRecipientSubmitted event to track
        // failed recipients and control the sending process.
        mailer.MessageRecipientSubmitted +=
            new SmtpMessageRecipientSubmittedEventHandler(OnRecipient);

        // Will succeed if at least To recipients are accepted.
        mailer.Send();

        // Report e-mail addresses of accepted and rejected recipients.
        foreach (EmailAddress addr in mailer.GetAcceptedRecipients())
        {
            Console.WriteLine("Successful e-mail: " + addr.Email);
        }
        foreach (EmailAddress addr in mailer.GetRefusedRecipients())
        {
            Console.WriteLine("Failed e-mail: " + addr.Email);
        }
    }
}
Imports System
Imports MailBee
Imports MailBee.Mime
Imports MailBee.SmtpMail

Module Module1
    Private Sub OnRecipient(ByVal sender As Object, _
          ByVal e As SmtpMessageRecipientSubmittedEventArgs)
        ' If the recipient was accepted, just move on.
        If e.Result Then
            Return
        End If

        ' Check if it was the To recipient who was rejected.
        Dim addr As EmailAddress
        For Each addr In e.MailMessage.To
            If addr.Email = e.RecipientEmail Then
                e.AllowRefusedRecipient = False
            End If
        Next addr
    End Sub

    Sub Main()
        Dim mailer As New Smtp()

        ' Use SMTP relay server with authentication.
        Dim server As SmtpServer = mailer.SmtpServers.Add( _
            "mail.example.com", "jane", "secret")

        ' Allow rejected recipients (the default state).
        server.AllowRefusedRecipients = True

        ' The message is not important to anyone but the boss.
        mailer.Message.From.Email = "jane@example.com"
        mailer.Message.Subject = "Notification"
        mailer.Message.BodyPlainText = "I'm going on vacation"

        ' We'll send the message to 1 important and 2 other addresses.
        mailer.Message.To.Add("boss@example.com", "Big Boss")
        mailer.Message.Cc.Add("mike@here.com", "Mike T.")
        mailer.Message.Cc.Add("john@there.com", "John D.")

        ' Subscribe to MessageRecipientSubmitted event to track
        ' failed recipients and control the sending process.
        AddHandler mailer.MessageRecipientSubmitted, AddressOf OnRecipient

        ' Will succeed if at least To recipients are accepted.
        mailer.Send()

        ' Report e-mail addresses of accepted and rejected recipients.
        Dim addr As EmailAddress
        For Each addr In mailer.GetAcceptedRecipients()
            Console.WriteLine("Successful e-mail: " & addr.Email)
        Next addr
        For Each addr In mailer.GetRefusedRecipients()
            Console.WriteLine("Failed e-mail: " & addr.Email)
        Next addr
    End Sub
End Module

Send feedback to AfterLogic

Copyright © 2006-2024 AfterLogic Corporation. All rights reserved.