SmtpTestSend Method
Tests sending a mail message to the recipients without actual submitting of the message data.

Namespace: MailBee.SmtpMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.4 build 677 for .NET 4.5
Syntax
public TestSendResult TestSend(
	SendFailureThreshold failureThreshold
)

Parameters

failureThreshold
Type: MailBee.SmtpMailSendFailureThreshold
Specifies whether the method should succeed if all recipients succeed, or at least one recipient succeeds (even if other recipients fail).

Return Value

Type: TestSendResult
OK if the sender and any or all (depends on failureThreshold value) recipients have been accepted by for delivery; otherwise, one of TestSendResult values specifying the error details.
Remarks

This method performs the actual attempt to connect to a mail server in order to check if the address exists. If you first need to check if the e-mail address string itself if of the correct format, use ValidateEmailAddressSyntax(String) static method.

If you need to test bulk of e-mail addresses, consider using EmailAddressValidator component.

When failureThreshold is Default, failed recipients are always allowed in direct send mode, and can be either disabled or enabled when submitting to SMTP relay server (depending on AllowRefusedRecipients property value). Other values of failureThreshold parameter allow the developer to override the default behavior for both direct send and SMTP relay send modes.

This method can be used prior to actual Send method call in order to make sure the sender and all recipients will be accepted during send mail operation. If any/all recipients fail, the method returns corresponding TestSendResult value. MailBeeException is not thrown. ErrorOccurred event is still raised, so the developer can obtain additional information about the error by handling this event.

TestSend(SendFailureThreshold) method is especially useful in direct send scenarios (discovering SMTP MX servers via DNS MX lookup rather than using predefined SMTP relay server). When no SMTP relay server is used, it's not possible to send to all recipients without fail, and cancel sending if at least one recipient fails (so that the message would be sent either to all recipients or to nobody). This is because the message may be sent to many SMTP MX servers in direct send mode, and if it was already sent to the first server and fails for the second one, it's not possible to tell the first server to rollback and cancel the recent mail submission. This is different from sending to SMTP relay server, where entire send mail operation is a single transaction. If it's cancelled, no recipients previously submitted will receive the message. When sending to an SMTP relay server, the developer can use AllowRefusedRecipients property to control whether the component should return an error if at least one recipient fails.

Note Note
The mail message submission may still fail on Send operation even if all recipients and the sender are valid. This typically occurs when the server accepts the sender and the recipients but rejects the message data (for instance, the message is too large, or the server decides the message contains spam or virus). Also, the server might be down at the moment when Send method gets called even if it was fine during TestSend(SendFailureThreshold) method call.
Examples
This sample sends a mail message in direct send mode (no SMTP relay server is used). To make sure all recipients will receive the message, TestSend(SendFailureThreshold) is called first. Extensive error checking is performed in ErrorOccurred event handler to learn why the error occurred.
using System;
using MailBee;
using MailBee.SmtpMail;
using MailBee.DnsMX;
using MailBee.Mime;

class Sample
{
    // ErrorOccurred event handler.
    private static void OnErrorOccurred(object sender, ErrorEventArgs e)
    {
        if (e.Reason is MailBeeSmtpRefusedRecipientException)
        {
            MailBeeSmtpRefusedRecipientException ex =
                e.Reason as MailBeeSmtpRefusedRecipientException;
            Console.WriteLine(ex.RefusedRecipientEmail + " was refused by the server");
        }
        else if (e.Reason is MailBeeDnsNameErrorException)
        {
            MailBeeDnsNameErrorException ex = e.Reason as MailBeeDnsNameErrorException;
            Console.WriteLine(ex.HostName + " is unknown host");
        }
        else if (e.Reason is MailBeeDnsProtocolNegativeResponseException)
        {
            MailBeeDnsProtocolNegativeResponseException ex =
                e.Reason as MailBeeDnsProtocolNegativeResponseException;
            Console.WriteLine("DNS server returned error for " + ex.HostName);
        }
        else if (e.Reason is MailBeeConnectionException)
        {
            MailBeeConnectionException ex = e.Reason as MailBeeConnectionException;
            if (ex.Protocol == TopLevelProtocolType.Smtp)
            {
                Console.WriteLine("Connection problem with " +
                    ex.RemoteHostName + " SMTP MX server ");
            }
        }
        else
        {
            Console.WriteLine(e.Reason.Message);
        }
    }

    // The actual code.
    static void Main(string[] args)
    {
        Smtp mailer = new Smtp();

        // Get DNS servers list from OS settings or config file 
        // (like app.config).
        mailer.DnsServers.Autodetect();

        mailer.From.AsString = "John Doe <jdoe@domain.com>";

        // Specify recipients.
        mailer.To.AsString = "no-display-name@website.com, \"Bill Smith, Jr.\" &lt;bill.smith@company.com&gt;";
        mailer.To.AddFromString("Kathy Smith &lt;k.smith@domain.com&gt;");
        mailer.To.AddFromString("email@address.com");
        mailer.To.Add("Mike Jackson, Sales Manager", "mj@company.com", "Sales Department");

        mailer.Subject = "Newsletter";
        mailer.BodyPlainText = "This is our weekly newsletter.";

        // Subscribe to the ErrorOccurred event.
        mailer.ErrorOccurred += new ErrorEventHandler(OnErrorOccurred);

        // Make sure all the recipients are ok.
        if (mailer.TestSend(SendFailureThreshold.AllRecipientsFailed) != TestSendResult.OK)
        {
            Console.WriteLine("No recipients can receive the message.");
        }
        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.");
            mailer.Send();
            Console.WriteLine("Sent to: " + mailer.GetAcceptedRecipients().ToString());
        }
    }
}
See Also