SmtpClient.OnQuit Event


Occurs when the client is disconnecting the smtp server.

[Visual Basic]
Public Event OnQuit As OnQuitEventHandler
Public Delegate Sub OnQuitEventHandler( _
    ByVal sender As Object, _
    ByRef cancel As Boolean _
)
[C#]
public event OnQuitEventHandler OnQuit;
public delegate void OnQuitEventHandler( 
     object sender, 
     ref bool cancel 
);
[C++]
public: __event OnQuitEventHandler^ OnQuit;
public __gc __delegate void OnQuitEventHandler( 
     Object^ sender, 
     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.
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++, JScript.NET] To get the full samples of EASendMail, please refer to Samples section.

[VB - Send Email with Event Handler]
Imports EASendMail
Module Module1
    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 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")
            oServer.User = "myusername"
            oServer.Password = "mypassword"

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

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

            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

            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 Event Handler]
using System;
using EASendMail;

namespace Test
{
    class Class1
    {
        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" );
        }

        [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");
                oServer.User = "myusername";
                oServer.Password = "mypassword";
                
                oMail.From = new MailAddress("from@adminsystem.com" );
                oMail.To.Add( new MailAddress("to@adminsystem.com" ));
                oMail.Subject = "test subject";
                oMail.TextBody = "test body";
                
                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.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 Event Handler]

using namespace System;
using namespace EASendMail;

public ref class SmtpClientEventHandler
{
public:
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" );
}
};

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

    try
    {
        SmtpServer ^oServer = gcnew SmtpServer("myserveraddres");
        oServer->User = "myusername";
        oServer->Password = "mypassword";
       
        oMail->From = gcnew MailAddress("from@adminsystem.com" );
        oMail->To->Add( gcnew MailAddress("to@adminsystem.com" ));
        oMail->Subject = "test subject";
        oMail->TextBody = "test body";
        
        oSmtp->OnAuthorized += gcnew SmtpClient::OnAuthorizedEventHandler( nullptr, SmtpClientEventHandler::OnAuthorized );
        oSmtp->OnIdle += gcnew SmtpClient::OnIdleEventHandler( nullptr, SmtpClientEventHandler::OnIdle );
        oSmtp->OnConnected += gcnew SmtpClient::OnConnectedEventHandler( nullptr, SmtpClientEventHandler::OnConnected );
        oSmtp->OnSendingDataStream += gcnew SmtpClient::OnSendingDataStreamEventHandler( nullptr, SmtpClientEventHandler::OnSendingDataStream );
        oSmtp->OnQuit += gcnew SmtpClient::OnQuitEventHandler( nullptr, SmtpClientEventHandler::OnQuit );
 
        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