SmtpClient.OnSendCommand Event


Occurs when the client has sent a command to the smtp server.

[Visual Basic]
Public Event OnSendCommand As OnSendCommandEventHandler
Public Delegate Sub OnSendCommandEventHandler( _
    ByVal sender As Object, _
    ByVal data() As Byte, _
    ByVal dataStream As Boolean, _
    ByRef cancel As Boolean _
)
[C#]
public event OnSendCommandEventHandler OnSendCommand;
public delegate void OnSendCommandEventHandler( 
     object sender, 
     byte[] data,
     bool dataStream,
     ref bool cancel 
);
[C++]
public: __event OnSendCommandEventHandler^ OnSendCommand;
public __gc __delegate void OnSendCommandEventHandler( 
     Object^ sender, 
     <unsigned char>^ data,
     bool dataStream,
     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 sent data.
dataStream
Gets a value indicating whether the sent data is the email content.
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 OnQuit( _
    ByVal sender As Object, _
    ByRef cancel As Boolean _
    )
        Console.WriteLine("Disconnecting ... ")
    End Sub

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

    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 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")

            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.OnConnected, AddressOf OnConnected
            AddHandler oSmtp.OnQuit, AddressOf OnQuit
            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 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 OnIdle( 
            object sender, 
            ref bool cancel 
        )
        {
            //Application.DoEvents();
        }

        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 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");

                oMail.From = new MailAddress("from@adminsystem.com" );
                oMail.To.Add( new MailAddress("to@adminsystem.com" ));
                oMail.Subject = "test subject";
                oMail.TextBody = "test body";
                
                oSmtp.OnIdle += new SmtpClient.OnIdleEventHandler( OnIdle );
                oSmtp.OnConnected += new SmtpClient.OnConnectedEventHandler( OnConnected );
                oSmtp.OnSendCommand += new SmtpClient.OnSendCommandEventHandler( OnSendCommand );
                oSmtp.OnReceiveResponse += new SmtpClient.OnReceiveResponseEventHandler( OnReceiveResponse );
                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, 
            bool __gc ^cancel 
            )
{
    Console::WriteLine( "Disconnecting ... " );
}

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

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 OnConnected( 
                 Object^ sender,  
                 bool __gc ^cancel 
                 )
{
    Console::Write( "Connected\r\n" );
}
};

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("to@adminsystem.com" ));
        oMail->Subject = "test subject";
        oMail->TextBody = "test body";
        
        oSmtp->OnIdle += gcnew SmtpClient::OnIdleEventHandler( nullptr, SmtpClientEventHandler::OnIdle );
        oSmtp->OnConnected += gcnew SmtpClient::OnConnectedEventHandler( nullptr, SmtpClientEventHandler::OnConnected );
        oSmtp->OnSendCommand += gcnew SmtpClient::OnSendCommandEventHandler( nullptr, SmtpClientEventHandler::OnSendCommand );
        oSmtp->OnReceiveResponse += gcnew SmtpClient::OnReceiveResponseEventHandler( nullptr, SmtpClientEventHandler::OnReceiveResponse );
        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