SmtpClient.OnReceiveResponse Event


Occurs when the client has received a response from smtp server.

[Visual Basic]
Public Event OnReceiveResponse As OnReceiveResponseEventHandler
Public Delegate Sub OnReceiveResponseEventHandler( _
    ByVal sender As Object, _
    ByVal data() As Byte, _
    ByRef cancel As Boolean _
)
[C#]
public event OnReceiveResponseEventHandler OnReceiveResponse;
public delegate void OnReceiveResponseEventHandler( 
     object sender, 
     byte[] data,
     ref bool cancel 
);
[C++]
public: __event OnReceiveResponseEventHandler^ OnReceiveResponse;
public __gc __delegate void OnReceiveResponseEventHandler( 
     Object^ sender, 
     array<unsigned char>^ data,
     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.
data
An array of type Byte that stores the received data.
cancel
Gets or sets a value indicating whether the task (sends email or tests email) should be canceled.

Remarks

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++] To get the full samples of EASendMail, please refer to Samples section.

[VB - Send Email with Event Handler]

Imports EASendMail

Module Module1

    Sub OnSendCommand( 
        ByVal sender As Object, 
        ByVal data() As Byte, 
        ByVal dataStream As Boolean, 
        ByRef cancel As Boolean 
        )
        If Not dataStream Then
            Console.Write(System.Text.Encoding.ASCII.GetString(data))
        End If
    End Sub

    Sub OnReceiveResponse( 
        ByVal sender As Object, 
        ByVal data() As Byte, 
        ByRef cancel As Boolean 
        )
        Console.Write(System.Text.Encoding.ASCII.GetString(data))
    End Sub

    Sub OnQuit(
        ByVal sender As Object,
        ByRef cancel As Boolean
        )
        Console.WriteLine("Disconnecting ... ")
    End Sub

    Sub OnAuthorized(
        ByVal sender As Object,
        ByRef cancel As Boolean
        )
        Console.WriteLine("Authorized")
    End Sub

    Sub OnIdle(
        ByVal sender As Object,
        ByRef cancel As Boolean
        )
        ' Application.DoEvents()
    End Sub

    Sub OnSendingDataStream(
        ByVal sender As Object,
        ByVal sent As Integer,
        ByVal total As Integer,
        ByRef cancel As Boolean
    )
        Console.WriteLine(String.Format("{0}/{1} sent", sent, total))
    End Sub

    Sub OnConnected(
        ByVal sender As Object,
        ByRef cancel As Boolean
    )
        Console.Write("Connected")
    End Sub

    Sub SendMail()

        Try
            Dim oServer As SmtpServer = New SmtpServer("myserveraddress")
            ' SMTP user authentication
            oServer.User = "myusername"
            oServer.Password = "mypassword"

            ' Most mordern SMTP servers require SSL/TLS connection now.
            ' ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically.
            oServer.ConnectType = SmtpConnectType.ConnectTryTLS

            Dim oMail As SmtpMail = New SmtpMail("TryIt")
            oMail.From = New MailAddress("from@adminsystem.com")
            oMail.To.Add(New MailAddress("to@adminsystem.com"))

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

            Dim oSmtp As SmtpClient = New SmtpClient

            AddHandler oSmtp.OnIdle, AddressOf OnIdle
            AddHandler oSmtp.OnAuthorized, AddressOf OnAuthorized
            AddHandler oSmtp.OnConnected, AddressOf OnConnected
            AddHandler oSmtp.OnQuit, AddressOf OnQuit
            AddHandler oSmtp.OnSendingDataStream, AddressOf OnSendingDataStream

            AddHandler oSmtp.OnReceiveResponse, AddressOf OnReceiveResponse
            AddHandler oSmtp.OnSendCommand, AddressOf OnSendCommand

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

        Catch exp As SmtpTerminatedException
            Console.WriteLine(exp.Message)
        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 Event Handler] using System; using EASendMail; namespace Test { class Class1 { public static void OnSendCommand( object sender, byte[] data, bool dataStream, ref bool cancel ) { if(!dataStream) { Console.Write(System.Text.Encoding.ASCII.GetString(data)); } } public static void OnReceiveResponse( object sender, byte[] data, ref bool cancel ) { Console.Write(System.Text.Encoding.ASCII.GetString(data)); } public static void OnQuit( object sender, ref bool cancel ) { Console.WriteLine("Disconnecting ... "); } public static void OnAuthorized( object sender, ref bool cancel ) { Console.WriteLine("Authorized"); } public static void OnIdle( object sender, ref bool cancel ) { // Application.DoEvents(); } public static void OnSendingDataStream( object sender, int sent, int total, ref bool cancel ) { Console.WriteLine(String.Format("{0}/{1} sent", sent, total)); } public static void OnConnected( object sender, ref bool cancel ) { Console.Write("Connected\r\n"); } static void SendMail() { try { SmtpServer oServer = new SmtpServer("myserveraddress"); // SMTP user authentication oServer.User = "myusername"; oServer.Password = "mypassword"; // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer.ConnectType = SmtpConnectType.ConnectTryTLS; SmtpMail oMail = new SmtpMail("TryIt"); oMail.From = new MailAddress("from@adminsystem.com"); oMail.To.Add(new MailAddress("to@adminsystem.com")); oMail.Subject = "test subject"; oMail.TextBody = "test body"; SmtpClient oSmtp = new SmtpClient(); oSmtp.OnAuthorized += new SmtpClient.OnAuthorizedEventHandler(OnAuthorized); oSmtp.OnIdle += new SmtpClient.OnIdleEventHandler(OnIdle); oSmtp.OnConnected += new SmtpClient.OnConnectedEventHandler(OnConnected); oSmtp.OnSendingDataStream += new SmtpClient.OnSendingDataStreamEventHandler(OnSendingDataStream); oSmtp.OnQuit += new SmtpClient.OnQuitEventHandler(OnQuit); oSmtp.OnSendCommand += new SmtpClient.OnSendCommandEventHandler(OnSendCommand); oSmtp.OnReceiveResponse += new SmtpClient.OnReceiveResponseEventHandler(OnReceiveResponse); oSmtp.SendMail(oServer, oMail); Console.WriteLine("Message was sent"); } catch (SmtpTerminatedException exp) { Console.WriteLine(exp.Message); } catch (Exception exp) { Console.WriteLine("Exception: {0}", exp.Message); } } static void Main(string[] args) { SendMail(); } } }
[C++/CLI - Send Email with Event Handler] using namespace System; using namespace EASendMail; static void OnSendCommand( Object^ sender, array<unsigned char>^ data, bool dataStream, Boolean % cancel ) { if(!dataStream) { System::Text::Encoding ^encoder = System::Text::Encoding::ASCII; Console::Write(encoder->GetString(data)); } } static void OnReceiveResponse( Object^ sender, array<unsigned char>^ data, Boolean % cancel ) { System::Text::Encoding ^encoder = System::Text::Encoding::ASCII; Console::Write(encoder->GetString(data)); } static void OnQuit( Object^ sender, Boolean % cancel ) { Console::WriteLine("Disconnecting ... "); } static void OnAuthorized( Object^ sender, Boolean % cancel ) { Console::WriteLine("Authorized"); } static void OnIdle( Object^ sender, Boolean % cancel ) { // Application::DoEvents(); } static void OnSendingDataStream( Object^ sender, int sent, int total, Boolean % cancel ) { Console::WriteLine("{0}/{1} sent", sent.ToString(), total.ToString()); } static void OnConnected( Object^ sender, Boolean % cancel ) { Console::Write("Connected\r\n"); } static void SendMail() { try { SmtpServer ^oServer = gcnew SmtpServer("myserveraddres"); // SMTP user authentication oServer->User = "myusername"; oServer->Password = "mypassword"; // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer->ConnectType = SmtpConnectType::ConnectTryTLS; SmtpMail ^oMail = gcnew SmtpMail("TryIt"); oMail->From = gcnew MailAddress("from@adminsystem.com"); oMail->To->Add(gcnew MailAddress("to@adminsystem.com")); oMail->Subject = "test subject"; oMail->TextBody = "test body"; SmtpClient ^oSmtp = gcnew SmtpClient(); oSmtp->OnAuthorized += gcnew SmtpClient::OnAuthorizedEventHandler(&OnAuthorized); oSmtp->OnIdle += gcnew SmtpClient::OnIdleEventHandler(&OnIdle); oSmtp->OnConnected += gcnew SmtpClient::OnConnectedEventHandler(&OnConnected); oSmtp->OnSendingDataStream += gcnew SmtpClient::OnSendingDataStreamEventHandler(&OnSendingDataStream); oSmtp->OnQuit += gcnew SmtpClient::OnQuitEventHandler(&OnQuit); oSmtp->OnSendCommand += gcnew SmtpClient::OnSendCommandEventHandler(&OnSendCommand); oSmtp->OnReceiveResponse += gcnew SmtpClient::OnReceiveResponseEventHandler(&OnReceiveResponse); oSmtp->SendMail(oServer, oMail); Console::WriteLine("message was sent"); } catch (SmtpTerminatedException ^exp) { Console::WriteLine(exp->Message); } catch (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