SmtpClient.OnSecuring Event


Occurs when the client is establishing the SSL connection.

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

[Visual Basic]
Imports EASendMail
Module Module1
    Sub OnQuit( _
    ByVal sender As Object, _
    ByRef cancel As Boolean _
    )
        Console.WriteLine("Disconnecting ... ")
    End Sub

    Sub OnSecuring( _
    ByVal sender As Object, _
    ByRef cancel As Boolean _
    )
        Console.WriteLine("Securing ... ")
    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"
            oServer.ConnectType = SmtpConnectType.ConnectSSLAuto

            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.OnSecuring, AddressOf OnSecuring
            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#]
using System;
using EASendMail;

namespace Test
{
    class Class1
    {
        public static void OnQuit( 
            object sender, 
            ref bool cancel 
        )
        {

            Console.WriteLine( "Disconnecting ... " );
        
        }

        public static void OnSecuring( 
            object sender, 
            ref bool cancel 
        )
        {
            Console.WriteLine( "Securing ... " );
        }

        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";
                oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

                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.OnSecuring += new SmtpClient.OnSecuringEventHandler( OnSecuring );
                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++]
#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;
using namespace EASendMail;

public __gc class SmtpClientEventHandler
{
public:
static void OnQuit( 
            Object* sender, 
            bool __gc *cancel 
            )
{
    Console::WriteLine( S"Disconnecting ... " );
}

static void OnSecuring( 
                Object* sender, 
                bool __gc *cancel 
                )
{
    Console::WriteLine( S"Securing ... " );
}

static void OnAuthorized( 
                  Object* sender, 
                  bool __gc *cancel 
                  )
{
    Console::WriteLine( S"Authorized" );
}

static void OnIdle( 
            Object* sender, 
            bool __gc *cancel 
            )
{
    //Application.DoEvents();
}

static void OnSendingDataStream( 
                         Object* sender, 
                         int sent, 
                         int total, 
                         bool __gc *cancel 
                         )
{
    Console::WriteLine( S"{0}/{1} sent", sent.ToString(), total.ToString());
}

static void OnConnected( 
                 Object* sender,  
                 bool __gc *cancel 
                 )
{
    Console::Write( S"Connected\r\n" );
}
};

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

    try
    {
        SmtpServer *oServer = new SmtpServer(S"myserveraddress");
        oServer->User = S"myusername";
        oServer->Password = S"mypassword";
        oServer->ConnectType = SmtpConnectType::ConnectSSLAuto;

        oMail->From = new MailAddress(S"from@adminsystem.com" );
        oMail->To->Add( new MailAddress(S"to@adminsystem.com" ));
        oMail->Subject = "test subject";
        oMail->TextBody = "test body";
        
        oSmtp->OnAuthorized += new SmtpClient::OnAuthorizedEventHandler( NULL, SmtpClientEventHandler::OnAuthorized );
        oSmtp->OnIdle += new SmtpClient::OnIdleEventHandler( NULL, SmtpClientEventHandler::OnIdle );
        oSmtp->OnConnected += new SmtpClient::OnConnectedEventHandler( NULL, SmtpClientEventHandler::OnConnected );
        oSmtp->OnSecuring += new SmtpClient::OnSecuringEventHandler( NULL, SmtpClientEventHandler::OnSecuring );
        oSmtp->OnSendingDataStream += new SmtpClient::OnSendingDataStreamEventHandler( NULL, SmtpClientEventHandler::OnSendingDataStream );
        oSmtp->OnQuit += new SmtpClient::OnQuitEventHandler( NULL, SmtpClientEventHandler::OnQuit );

        oSmtp->SendMail( oServer, oMail );
        Console::WriteLine( S"message was sent" );
    }
    catch( EASendMail::SmtpTerminatedException *exp )
    {
        Console::WriteLine( exp->Message );
    }
    catch( EASendMail::SmtpServerException *exp )
    {
        Console::WriteLine( S"Exception: Server Respond: {0}", exp->ErrorMessage );
    }
    catch( System::Net::Sockets::SocketException *exp )
    {
        Console::WriteLine( S"Exception: Networking Error: {0} {1}", exp->ErrorCode.ToString(S"d"), exp->Message );
    }
    catch( System::ComponentModel::Win32Exception *exp )
    {
        Console::WriteLine( S"Exception: System Error: {0} {1}", exp->ErrorCode.ToString(S"d"), exp->Message );           
    }
    catch( System::Exception *exp )
    {
        Console::WriteLine( S"Exception: Common: {0}", exp->Message );           
    }
}

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