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 SendMail()

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

            Dim oMail As SmtpMail = New SmtpMail("TryIt")
            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"

            Dim oSmtp As SmtpClient = New SmtpClient()
            AddHandler oSmtp.OnRcptToError, AddressOf OnRcptToError

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

        Catch exp As Exception
            Console.WriteLine("Exception: {0}", exp.Message)
        End Try
    End Sub

    Sub Main()
        SendMail()
    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; } } static void SendMail() { try { SmtpServer oServer = new SmtpServer("myserveraddress"); SmtpMail oMail = new SmtpMail("TryIt"); 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"; SmtpClient oSmtp = new SmtpClient(); oSmtp.OnRcptToError += new SmtpClient.OnRcptToErrorEventHandler(OnRcptToError); oSmtp.SendMail( oServer, oMail ); Console.WriteLine( "Message was sent" ); } catch(Exception exp) { Console.WriteLine("Exception: {0}", exp.Message); } } [STAThread] static void Main(string[] args) { SendMail(); } } }
[C++/CLI - Send Email with OnRcptToError Event] using namespace System; using namespace System::Windows::Forms; using namespace EASendMail; 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; } } static void SendMail() { try { SmtpServer ^oServer = gcnew SmtpServer("myserveraddres"); SmtpMail ^oMail = gcnew SmtpMail("TryIt"); 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"; SmtpClient ^oSmtp = gcnew SmtpClient(); oSmtp->OnRcptToError += gcnew SmtpClient::OnRcptToErrorEventHandler(&OnRcptToError); oSmtp->SendMail(oServer, oMail); Console::WriteLine("message was sent"); } catch(System::Exception ^exp) { Console::WriteLine("Exception: {0}", exp->Message); } } int main(array<String ^> ^args) { 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