MailClient.OnSendCommand Event


Occurs when the client has sent a command to the mail 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 __gc[],
     bool __gc *dataStream,
     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 (MailClient 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 should be canceled.

Remarks

If cancel parameter is set to true in the event, the client terminates the connection immediately and a ClientTerminatedException exception will be thrown.

Example

[Visual Basic, C#, C++] To get the full samples of EAGetMail, please refer to Samples section.

[Visual Basic]
Imports EAGetMail

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, _
    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 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 OnConnected( _
    ByVal sender As Object, _
    ByRef cancel As Boolean _
    )
        Console.WriteLine("Connected")
    End Sub
    
    Public Sub ReceiveMail( _
    ByVal sServer As String, _
    ByVal sUserName As String, _
    ByVal sPassword As String, _
    ByVal bSSLConnection As Boolean)

        Dim oClient As New MailClient("TryIt")
        'To receive email from imap4 server, please change
        'ServerProtocol.Pop3 to ServerProtocol.Imap4 in MailServer constructor

        Dim oServer As New MailServer(sServer, _
            sUserName, sPassword, bSSLConnection, _
            ServerAuthType.AuthLogin, ServerProtocol.Pop3)


        'by default, the pop3 port is 110, imap4 port is 143, 
        'the pop3 ssl port is 995, imap4 ssl port is 993
        'you can also change the port like this
        'oServer.Port = 110

        'Catching the following events is not necessary, 
        'just make the application more user friendly.
        'If you use the object in asp.net/windows service or non-gui application, 
        'You need not to catch the following events.
        'To learn more detail, please refer to the code in EAGetMail EventHandler region

        AddHandler oClient.OnAuthorized, AddressOf OnAuthorized
        AddHandler oClient.OnConnected, AddressOf OnConnected
        AddHandler oClient.OnIdle, AddressOf OnIdle
        AddHandler oClient.OnQuit, AddressOf OnQuit
        AddHandler oClient.OnReceiveResponse, AddressOf OnReceiveResponse
        AddHandler oClient.OnSendCommand, AddressOf OnSendCommand

        Try
            oClient.Connect(oServer)
            Dim infos() As MailInfo = oClient.GetMailInfos()
            Dim count As Integer = infos.Length
            For i As Integer = 0 To count - 1
                Dim info As MailInfo = infos(i)
                Dim oMail As Mail = oClient.GetMail(info)
                'Save mail to local file
                oMail.SaveAs(String.Format("c:\temp\{0}.eml", i), True)
            Next

            For i As Integer = 0 To count - 1
                Dim info As MailInfo = infos(i)
                oClient.Delete(info)
            Next

            ' Delete method just mark the email as deleted, 
            ' Quit method pure the emails from server exactly.
            oClient.Quit()
        Catch ep As MailServerException
            'Message contains the information returned by mail server
            Console.WriteLine("Server Respond: {0}", ep.Message)
        Catch ep As System.Net.Sockets.SocketException
            Console.WriteLine("Socket Error: {0}", ep.Message)
        Catch ep As System.ComponentModel.Win32Exception
            Console.WriteLine("Win32 Error: {0}", ep.Message)
        Catch ep As Exception
            Console.WriteLine("System Error: {0}", ep.Message)
        End Try

        oClient.Close()
    End Sub
    
    Sub Main()
        ReceiveMail("myserver", "myuser", "mypassword", False)
    End Sub    
End Module

[C#]
using System;
using EAGetMail;

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,
            bool dataStream,    
            ref bool cancel 
        )
        {
            if( !dataStream )
            {
                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 OnConnected( 
            object sender,  
            ref bool cancel 
        )
        {
            Console.WriteLine( "Connected" );
        }

        public static void ReceiveMail( 
            string sServer, 
            string sUserName,
            string sPassword,
            bool bSSLConnection)
        {
            MailClient oClient = new MailClient("TryIt");
            //To receive email from imap4 server, please change
            //ServerProtocol.Pop3 to ServerProtocol.Imap4 in MailServer constructor

            MailServer oServer  = new MailServer(sServer, 
                sUserName, sPassword, bSSLConnection, 
                ServerAuthType.AuthLogin, ServerProtocol.Pop3);

            //by default, the pop3 port is 110, imap4 port is 143, 
            //the pop3 ssl port is 995, imap4 ssl port is 993
            //you can also change the port like this
            //oServer.Port = 110;
            try
            {
                //Catching the following events is not necessary, 
                //just make the application more user friendly.
                //If you use the object in asp.net/windows service or non-gui application, 
                //You need not to catch the following events.
                //To learn more detail, please refer to the code in EAGetMail EventHandler region
                oClient.OnAuthorized += new MailClient.OnAuthorizedEventHandler( OnAuthorized );
                oClient.OnConnected += new MailClient.OnConnectedEventHandler( OnConnected );
                oClient.OnIdle += new MailClient.OnIdleEventHandler( OnIdle );
                oClient.OnQuit += new MailClient.OnQuitEventHandler( OnQuit );
                oClient.OnSendCommand += new MailClient.OnSendCommandEventHandler( OnSendCommand );
                oClient.OnReceiveResponse +=    
                new MailClient.OnReceiveResponseEventHandler( OnReceiveResponse ); oClient.Connect(oServer); MailInfo
                [] infos=    oClient.GetMailInfos();
                int count=    

                
                  infos.Length;  for(
                int i = 
                  0;  i <  count; i++ )
                {
                    MailInfo info = infos[i];
                    Mail oMail = oClient.GetMail(info);
                    //Save mail to local    file
                    oMail.SaveAs(String.Format("c:\\{0}.eml", i), true);
                }

                for( int i =
                      
                

                  0;  i <  count; i++ )
                {
                    MailInfo info = infos[i];
                    oClient.Delete(info);
                }
            
                // Delete method just mark the email as deleted, 
                // Quit method pure the emails from server exactly.
                oClient.Quit();
            }
            catch( MailServerException ep ) 
            {
                //Message contains the information returned by mail server
                Console.WriteLine("Server Respond: {0}", ep.Message);
            }
            catch( System.Net.Sockets.SocketException ep ) 
            {
                Console.WriteLine("Socket Error: {0}", ep.Message);
            }
            catch( Exception ep ) 
            {
                Console.WriteLine("System Error: {0}", ep.Message);
            }

            oClient.Close();
        }
        
        [STAThread]
        static void Main(string[] args)
        {
            ReceiveMail( "myserver", "myusername", "mypassword", false );
        }
    }
}

[C++]
#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;
using namespace EAGetMail;

public __gc class MailClientEventHandler
{
public:
static void OnSendCommand( 
                   Object* sender, 
                   unsigned char data __gc[], 
                   bool dataStream,  
                   bool __gc *cancel 
                   )
{
    if( !dataStream )
    {
        System::Text::Encoding *encoder = System::Text::Encoding::ASCII;
        Console::Write(encoder->GetString( data ));
    }
}

static void OnReceiveResponse( 
                       Object* sender, 
                       unsigned char data __gc[], 
                       bool dataStream,  
                       bool __gc *cancel 
                       )
{
    if( !dataStream )
    {
        System::Text::Encoding *encoder = System::Text::Encoding::ASCII;
        Console::Write(encoder->GetString( data ));
    }
}

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

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

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

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

Void ReceiveMail( 
    String *sServer, 
    String *sUserName,
    String *sPassword,
    bool bSSLConnection)
{
    MailClient *oClient = new MailClient(S"TryIt");
    //To receive email from imap4 server, please change
    //ServerProtocol::Pop3 to ServerProtocol::Imap4 in MailServer constructor

    MailServer *oServer  = new MailServer(sServer, 
        sUserName, sPassword, bSSLConnection, 
        ServerAuthType::AuthLogin, ServerProtocol::Pop3);

    //by default, the pop3 port is 110, imap4 port is 143, 
    //the pop3 ssl port is 995, imap4 ssl port is 993
    //you can also change the port like this
    //oServer->Port = 110;
    try
    {
        //Catching the following events is not necessary, 
        //just make the application more user friendly.
        //If you use the object in asp.net/windows service or non-gui application, 
        //You need not to catch the following events.
        //To learn more detail, please refer to the code in EAGetMail EventHandler region
    
        oClient->OnAuthorized += new MailClient::OnAuthorizedEventHandler( NULL, MailClientEventHandler::OnAuthorized );
        oClient->OnIdle += new MailClient::OnIdleEventHandler( NULL, MailClientEventHandler::OnIdle );
        oClient->OnQuit += new MailClient::OnQuitEventHandler( NULL, MailClientEventHandler::OnQuit );
    
        oClient->OnConnected += new MailClient::OnConnectedEventHandler( NULL, MailClientEventHandler::OnConnected );
        oClient->OnSendCommand += new MailClient::OnSendCommandEventHandler( NULL, MailClientEventHandler::OnSendCommand );
        oClient->OnReceiveResponse +=     
         new MailClient::OnReceiveResponseEventHandler( NULL, MailClientEventHandler::OnReceiveResponse ); oClient->Connect(oServer);
        MailInfo *infos[]=     oClient->GetMailInfos();
        int count=     

        
         infos->Length; for(
        int i = 
          0;  i <  count; i++ )
        {
            MailInfo *info = infos[i];
            Mail *oMail = oClient->GetMail(info);
            //Save mail to local file
            oMail->SaveAs(String::Format(S"c:\\{0}.eml", __box(i)), true);
        }

        for( int i =
              
        

          0;  i <  count; i++ )
        {
            MailInfo *info = infos[i];
            oClient->Delete(info);
        }
    
        // Delete method just mark the email as deleted, 
        // Quit method pure the emails from server exactly.
        oClient->Quit();
    }
    catch( MailServerException *ep ) 
    {
        //Message contains the information returned by mail server
        Console::WriteLine( S"Server Respond: {0}", ep->Message);
    }
    catch( System::Net::Sockets::SocketException *ep ) 
    {
        Console::WriteLine( S"Socket Error: {0}", ep->Message);
    }
    catch( Exception *ep ) 
    {
        Console::WriteLine( S"System Error: {0}", ep->Message);
    }

    oClient->Close();
}

int _tmain()
{
    ReceiveMail( S"myserver", S"myuser", S"mypassword", false );
    return 0;
}