SmtpClient.OnRcptToError Event


Occurs when the smtp server rejected a recipient.

[Visual Basic]
Public Event OnRcptToError As OnRcptToErrorEventHandler
Public Delegate Sub OnRcptToErrorEventHandler( _
    ByVal sender As Object, _
    ByVal errorStr As String, _
    ByVal rcpt As String, _
    ByRef sendnext As Boolean, _
    ByRef cancel As Boolean _
)
[C#]
public event OnRcptToErrorEventHandler OnRcptToError;
public delegate void OnRcptToErrorEventHandler( 
     object sender, 
     string error,
     string rcpt,
     ref bool sendnext,
     ref bool cancel 
);
[C++]
public: __event OnRcptToErrorEventHandler^ OnRcptToError;
public __gc __delegate void OnRcptToErrorEventHandler( 
     Object^ sender, 
     String^ error,
     String^ rcpt,
     Boolean % sendnext,
     Boolean % cancel
);
[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

sender
The source (SmtpClient instance) of the event.
error
The error response returned by smtp server.
rcpt
The email address rejected by smtp server.
sendnext
Gets or sets a value indicating whether the next email address should be sent to smtp server.
cancel
Gets or sets a value indicating whether the task (sends email or tests email) should be canceled.

Remarks

By default, if there are multiple recipients in one email message and some email addresses are rejected by smtp server, the SmtpClient throws the SmtpServerException exception. However, if this event is catched, the sendnext parameter is set to true, SmtpClient continues to send the email to next recipient.
If cancel parameter is set to true in the event, the client terminates the sending immediately and a SmtpTerminatedException exception will be thrown.

Example

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

[VB - Send Email with OnRcptToError Event]
Imports System.Windows.Forms
Imports EASendMail
Module Module1

    Sub OnRcptToError( _
    ByVal sender As Object, _
    ByVal errorstr As String, _
    ByVal rcpt As String, _
    ByRef sendnext As Boolean, _
    ByRef cancel As Boolean _
    )
        If MessageBox.Show(String.Format("{0} is rejected by server: {1}, are you want to continue to send the email?", _
                rcpt, errorstr), "Warning", MessageBoxButtons.OKCancel) = DialogResult.OK Then
            sendnext = True
        End If
    End Sub

    Sub Main()
        SendMail()
    End Sub

    Sub SendMail()
        Dim oMail As SmtpMail = New SmtpMail("TryIt")
        Dim oSmtp As SmtpClient = New SmtpClient

        Try
            Dim oServer As SmtpServer = New SmtpServer("myserveraddress")

            oMail.From = New MailAddress("from@adminsystem.com")
            oMail.To.Add(New MailAddress("good@adminsystem.com"))
            oMail.To.Add(New MailAddress("bad@adminsystem.com))

            oMail.Subject = "test subject"
            oMail.TextBody = "test body"

            AddHandler oSmtp.OnRcptToError, AddressOf OnRcptToError

            oSmtp.SendMail(oServer, oMail)
            Console.WriteLine("Message was sent")

        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
    End Sub
End Module

[C# - Send Email with OnRcptToError Event]
using System;
using System.Windows.Forms;
using EASendMail;

namespace Test
{
    class Class1
    {
        public static void OnRcptToError( 
            object sender, 
            string error,
            string rcpt,
            ref bool sendnext,
            ref bool cancel 
        )
        {
            if( MessageBox.Show(String.Format("{0} is rejected by server: {1}, are you want to continue to send the email?", 
                rcpt, error), "Warning", MessageBoxButtons.OKCancel) == DialogResult.OK )
            {
                sendnext = true;
            }        
        }

        [STAThread]
        static void Main(string[] args)
        {
            SendMail();
        }

        static void SendMail()
        {
            SmtpMail oMail = new SmtpMail("TryIt");
            SmtpClient oSmtp = new SmtpClient();

            try
            {
                SmtpServer oServer = new SmtpServer("myserveraddress");

                oMail.From = new MailAddress("from@adminsystem.com" );
                oMail.To.Add( new MailAddress("good@adminsystem.com" ));
                oMail.To.Add( new MailAddress("bad@adminsystem.com" ));
                oMail.Subject = "test subject";
                oMail.TextBody = "test body";
                
                oSmtp.OnRcptToError += new SmtpClient.OnRcptToErrorEventHandler( OnRcptToError );
                oSmtp.SendMail( oServer, oMail );

                Console.WriteLine( "Message was  sent" );
            }
            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 );         
            }
        }
    }
}

[C++/CLI - Send Email with OnRcptToError Event]

using namespace System;
using namespace System::Windows::Forms;
using namespace EASendMail;

public ref class SmtpClientEventHandler
{
public:
static void OnRcptToError( 
                   Object^ sender, 
                   String^ error,
                   String^ rcpt,
                   Boolean % sendnext,
                   Boolean % cancel 
                   )
{
    if( MessageBox::Show(String::Format("{0} is rejected by server: {1}, are you want to continue to send the email?", 
        rcpt, error), "Warning", MessageBoxButtons::OKCancel) == DialogResult::OK )
    {
        sendnext = true;
    }
}
};

void SendMail()
{
    SmtpMail ^oMail = gcnew SmtpMail("TryIt");
    SmtpClient ^oSmtp = gcnew SmtpClient();

    try
    {
        SmtpServer ^oServer = gcnew SmtpServer("myserveraddres");

        oMail->From = gcnew MailAddress("from@adminsystem.com" );
        oMail->To->Add( gcnew MailAddress("good@adminsystem.com" ));
        oMail->To->Add( gcnew MailAddress("bad@adminsystem.com" ));
        oMail->Subject = "test subject";
        oMail->TextBody = "test body";
        
        oSmtp->OnRcptToError += gcnew SmtpClient::OnRcptToErrorEventHandler( nullptr, SmtpClientEventHandler::OnRcptToError );

        oSmtp->SendMail( oServer, oMail );
        Console::WriteLine( "message was sent" );
    }
    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 );           
    }
}

int _tmain()
{
    SendMail();
    return 0;
}

See Also

SmtpClient.SendMail Method
SmtpClient.BeginSendMail Method

Online Tutorials

Send Email with Event Handler in C# - Tutorial
Send Email with Event Handler in VB - Tutorial
Send Email with Event Handler in Managed C++/CLI - Tutorial