SmtpClient.EndTestRecipients Method


Ends a pending asynchronous test.

[Visual Basic]
Public Sub EndTestRecipients( _
    ar As SmtpClientAsyncResult _
)
[C#]
public void EndTestRecipients(
    SmtpClientAsyncResult ar
);
[C++]
public: void EndTestRecipients(
    SmtpClientAsyncResult* ar
);
[JScript]
public function EndTestRecipients( 
    ar : SmtpClientAsyncResult
);

Parameters

ar
An SmtpClientAsyncResult that stores state information for this asynchronous operation.

Remarks

How does it work? Firstly, SmtpClient performs a DNS MX record query. If it retrieves the recipient's local SMTP server successfully, SmtpClient will try to connect to this server. SmtpClient then performs "RCPT TO" command to test if this SMTP server accept this email address. 

Please always pass null (Nothing in Visual Basic) to SmtpServer paramter in BeginTestRecipients and TestRecipients methods except you want to test whether an email address will be accepted by a specified SMTP server.

Example

[Visual Basic, C#, C++] To get the full samples of EASendMail, please refer to Samples section.

[VB - Test Email Address]
Imports EASendMail
Sub TestEmailAddress()
    Dim oMail As SmtpMail = New SmtpMail("TryIt")
    Dim oSmtp As SmtpClient = New SmtpClient
    Dim ar As SmtpClientAsyncResult = Nothing

    Try
        oMail.From = New MailAddress("from@adminsystem.com")
        oMail.To.Add(New MailAddress("to@adminsystem.com"))
        
        ar = oSmtp.BeginTestRecipients(Nothing, oMail, Nothing, Nothing)
        Do While Not ar.AsyncWaitHandle.WaitOne(5, False)
            'do other thing
        Loop

        oSmtp.EndTestRecipients(ar)
        Console.WriteLine("PASS")
        
    Catch exp As SmtpTerminatedException
        Console.WriteLine(exp.Message)
    Catch exp As SmtpServerException
        Console.WriteLine("Exception: Server Respond: {0}", exp.ErrorMessage)
    Catch exp As System.Net.Sockets.SocketException
        Console.WriteLine("Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message)
    Catch exp As System.ComponentModel.Win32Exception
        Console.WriteLine("Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message)
    Catch exp As System.Exception
        Console.WriteLine("Exception: Common: {0}", exp.Message)
    End Try

    Console.WriteLine("SMTP LOG:" & vbCrLf)
    Console.WriteLine(ar.SmtpClientInstance.SmtpConversation)
End Sub

[C# - Test Email Address]
using System;
using EASendMail;
void TestEmailAddress()
{
    SmtpMail oMail = new SmtpMail("TryIt");
    SmtpClient oSmtp = new SmtpClient();
    SmtpClientAsyncResult ar = null;
    try
    {
        oMail.From = new MailAddress("from@adminsystem.com" );
        oMail.To.Add( new MailAddress("to@adminsystem.com" ));
        
        ar = oSmtp.BeginTestRecipients( null, oMail, null, null );
        while(!ar.AsyncWaitHandle.WaitOne(5, false ))
        {
            //do other thing
        }

        oSmtp.EndTestRecipients( ar );

        Console.WriteLine( "PASS" );
    }
    catch( SmtpTerminatedException exp )
    {
        Console.WriteLine( exp.Message );
    }
    catch( SmtpServerException exp )
    {
        Console.WriteLine( "Exception: Server Respond: {0}", exp.ErrorMessage );
    }
    catch( System.Net.Sockets.SocketException exp )
    {
        Console.WriteLine( "Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message );
    }
    catch( System.ComponentModel.Win32Exception exp )
    {
        Console.WriteLine( "Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message );            
    }
    catch( System.Exception exp )
    {
        Console.WriteLine( "Exception: Common: {0}", exp.Message );         
    }

    Console.WriteLine( "SMTP LOG:\r\n" );
    Console.WriteLine( ar.SmtpClientInstance.SmtpConversation );
}
        
[C++/CLI - Test Email Address]
using namespace System;
using namespace EASendMail;

void TestEmailAddress()
{
    SmtpMail ^oMail = gcnew SmtpMail("TryIt");
    SmtpClient ^oSmtp = gcnew SmtpClient();
    SmtpClientAsyncResult ^ar = NULL;

    try
    {
        oMail->From = gcnew MailAddress("from@adminsystem.com" );
        oMail->To->Add( gcnew MailAddress("to@adminsystem.com" ));
        
        ar = oSmtp->BeginTestRecipients( NULL, oMail, NULL, NULL );
        while(!ar->AsyncWaitHandle->WaitOne( 5, false ))
        {
            //do other thing;
        }

        oSmtp->EndTestRecipients( ar );
        Console::WriteLine( "PASS" );
    
    }
    catch( EASendMail::SmtpTerminatedException ^exp )
    {
        Console::WriteLine( exp->Message );
    }
    catch( EASendMail::SmtpServerException ^exp )
    {
        Console::WriteLine( "Exception: Server Respond: {0}", exp->ErrorMessage );
    }
    catch( System::Net::Sockets::SocketException ^exp )
    {
        Console::WriteLine( "Exception: Networking Error: {0} {1}", exp->ErrorCode.ToString("d"), exp->Message );
    }
    catch( System::ComponentModel::Win32Exception ^exp )
    {
        Console::WriteLine( "Exception: System Error: {0} {1}", exp->ErrorCode.ToString("d"), exp->Message );           
    }
    catch( System::Exception ^exp )
    {
        Console::WriteLine( "Exception: Common: {0}", exp->Message );           
    }

    Console::WriteLine( "SMTP LOG:\r\n" );
    Console::WriteLine( ar->SmtpClientInstance->SmtpConversation );
}

Test Email Address

See Also

SmtpClient.BeginTestRecipients Method
Bulk Email Sender Guidelines
Process Bounced Email (Non-Delivery Report) and Email Tracking