MailClient.OnReceiveResponse Event


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

[Visual Basic]
Public Event OnReceiveResponse As OnReceiveResponseEventHandler
Public Delegate Sub OnReceiveResponseEventHandler( _ ByVal sender As Object,
    _ ByVal data() As Byte, _ ByVal dataStream As Boolean, _ ByRef cancel
    As Boolean _ )
[C#]
public event OnReceiveResponseEventHandler OnReceiveResponse;
public delegate void OnReceiveResponseEventHandler( object sender, byte[]
    data, bool dataStream, ref bool cancel );
[C++]
public: __event OnReceiveResponseEventHandler^ OnReceiveResponse;
public __gc __delegate void OnReceiveResponseEventHandler( Object^ sender,
    array<unsigned char^> data, bool dataStream, bool % 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 received data.
dataStream
Gets a value indicating whether the receiving 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.

[VB - Retrieve email from server with event handler]
Imports System
Imports System.Globalization
Imports System.IO
Imports System.Text
Imports EAGetMail

Public Class TestClass

    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 OnReceivingDataStream(
    ByVal sender As Object,
    ByVal info As MailInfo,
    ByVal received As Integer,
    ByVal total As Integer,
    ByRef cancel As Boolean)
        Console.WriteLine(String.Format("{0}/{1} received", received, total))
    End Sub

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

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

    ' Generate an unqiue email file name based on date time.
    Shared Function _generateFileName(ByVal sequence As Integer) As String
        Dim currentDateTime As DateTime = DateTime.Now
        Return String.Format("{0}-{1:000}-{2:000}.eml",
                            currentDateTime.ToString("yyyyMMddHHmmss", New CultureInfo("en-US")),
                            currentDateTime.Millisecond,
                            sequence)
    End Function

    Public Sub ReceiveMail(server As String, user As String, password As String, useSsl As Boolean)

        Try
            ' Create a folder named "inbox" under current directory
            ' to save the email retrieved.
            Dim localInbox As String = String.Format("{0}\inbox", Directory.GetCurrentDirectory())

            ' If the folder is not existed, create it.
            If Not Directory.Exists(localInbox) Then
                Directory.CreateDirectory(localInbox)
            End If

            ' To receive email from IMAP4 server, please change
            ' ServerProtocol.Pop3 to ServerProtocol.Imap4 in MailServer constructor

            ' To receive email with Exchange Web Service, please change
            ' ServerProtocol.Pop3 to ServerProtocol.ExchangeEWS to MailServer constructor

            ' To receive email with Exchange WebDAV, please change
            ' ServerProtocol.Pop3 to ServerProtocol.ExchangeWebDAV to MailServer constructor

            ' Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007
            ' Or later version, POP3/IMAP4 service Is disabled by default. If you don't want to use POP3/IMAP4
            ' to download email from Exchange Server, you can use Exchange Web Service (Exchange 2007-2019 Or
            ' later version) Or WebDAV (Exchange 2000/2003) protocol.

            ' Most modern email server require SSL/TLS connection, 
            ' set useSsl to true Is recommended.
            Dim oServer As New MailServer(server, user, password, useSsl,
                        ServerAuthType.AuthLogin, ServerProtocol.Pop3)

            ' POP3 port Is 110, POP3 SSL port Is 995
            ' IMAP4 port Is 143,  IMAP4 SSL port Is 993.
            ' EWS/WebDAV, please ignore Port property.
            If oServer.Protocol = ServerProtocol.Pop3 Then
                oServer.Port = If(useSsl, 995, 110)
            ElseIf oServer.Protocol = ServerProtocol.Imap4 Then
                oServer.Port = If(useSsl, 993, 143)
            End If

            Console.WriteLine("Connecting server ...")
            Dim oClient As New MailClient("TryIt")

            ' 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.
            AddHandler oClient.OnAuthorized, AddressOf OnAuthorized
            AddHandler oClient.OnConnected, AddressOf OnConnected
            AddHandler oClient.OnIdle, AddressOf OnIdle
            AddHandler oClient.OnQuit, AddressOf OnQuit
            AddHandler oClient.OnReceivingDataStream, AddressOf OnReceivingDataStream
            AddHandler oClient.OnSecuring, AddressOf OnSecuring
            
            AddHandler oClient.OnSendCommand, AddressOf OnSendCommand
            AddHandler oClient.OnReceiveResponse, AddressOf OnReceiveResponse

            oClient.Connect(oServer)

            Console.WriteLine("Retrieving email list ...")
            Dim infos() As MailInfo = oClient.GetMailInfos()

            Console.WriteLine("Total {0} email(s)", infos.Length)

            For i As Integer = 0 To infos.Length - 1
                Console.WriteLine("Checking {0}/{1} ...", i + 1, infos.Length)
                Dim info As MailInfo = infos(i)

                ' Generate an unqiue email file name based on date time.
                Dim fileName As String = _generateFileName(i + 1)
                Dim fullPath As String = String.Format("{0}\{1}", localInbox, fileName)

                Console.WriteLine("Downloading {0}/{1} ...", i + 1, infos.Length)
                Dim oMail As Mail = oClient.GetMail(info)

                ' Save email to local disk
                oMail.SaveAs(fullPath, True)

                ' Mark email as deleted on server.
                Console.WriteLine("Deleting ... {0}/{1}", i + 1, infos.Length)
                oClient.Delete(info)
            Next

            Console.WriteLine("Disconnecting ...")
            ' Delete method just mark the email as deleted, 
            ' Quit method expunge the emails from server permanently.
            oClient.Quit()

            Console.WriteLine("Completed!")
        Catch ep As Exception
            Console.WriteLine("Error: {0}", ep.Message)
        End Try

    End Sub
End Class


[C# - Retrieve email from server with event handler] using System; using System.IO; using System.Globalization; using System.Text; using EAGetMail; class TestClass { 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 OnReceivingDataStream( object sender, MailInfo info, int received, int total, ref bool cancel ) { Console.WriteLine(String.Format("{0}/{1} received", received, total)); } public static void OnConnected( object sender, ref bool cancel ) { Console.WriteLine("Connected"); } public static void OnSecuring( object sender, ref bool cancel ) { Console.WriteLine("Securing ..."); } // Generate an unqiue email file name based on date time static string _generateFileName(int sequence) { DateTime currentDateTime = DateTime.Now; return string.Format("{0}-{1:000}-{2:000}.eml", currentDateTime.ToString("yyyyMMddHHmmss", new CultureInfo("en-US")), currentDateTime.Millisecond, sequence); } public void ReceiveMail(string server, string user, string password, bool useSsl) { try { // Create a folder named "inbox" under current directory // to save the email retrieved. string localInbox = string.Format("{0}\\inbox", Directory.GetCurrentDirectory()); // If the folder is not existed, create it. if (!Directory.Exists(localInbox)) { Directory.CreateDirectory(localInbox); } // To receive email from IMAP4 server, please change // ServerProtocol.Pop3 to ServerProtocol.Imap4 in MailServer constructor // To receive email with Exchange Web Service, please change // ServerProtocol.Pop3 to ServerProtocol.ExchangeEWS to MailServer constructor // To receive email with Exchange WebDAV, please change // ServerProtocol.Pop3 to ServerProtocol.ExchangeWebDAV to MailServer constructor // Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007 // or later version, POP3/IMAP4 service is disabled by default. If you don't want to use POP3/IMAP4 // to download email from Exchange Server, you can use Exchange Web Service (Exchange 2007-2019 or // later version) or WebDAV (Exchange 2000/2003) protocol. // Most modern email server require SSL/TLS connection, // set useSsl to true is recommended. MailServer oServer = new MailServer(server, user, password, useSsl, ServerAuthType.AuthLogin, ServerProtocol.Pop3); // POP3 port is 110, POP3 SSL port is 995 // IMAP4 port is 143, IMAP4 SSL port is 993. // EWS/WebDAV, please ignore Port property. if (oServer.Protocol == ServerProtocol.Pop3) { oServer.Port = (useSsl) ? 995 : 110; } else if (oServer.Protocol == ServerProtocol.Imap4) { oServer.Port = (useSsl) ? 993 : 143; } Console.WriteLine("Connecting server ..."); MailClient oClient = new MailClient("TryIt"); // 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.OnReceivingDataStream += new MailClient.OnReceivingDataStreamEventHandler(OnReceivingDataStream); oClient.OnSecuring += new MailClient.OnSecuringEventHandler(OnSecuring); oClient.OnSendCommand += new MailClient.OnSendCommandEventHandler(OnSendCommand); oClient.OnReceiveResponse += new MailClient.OnReceiveResponseEventHandler(OnReceiveResponse); oClient.Connect(oServer); Console.WriteLine("Retreiving email list ..."); MailInfo[] infos = oClient.GetMailInfos(); Console.WriteLine("Total {0} email(s)", infos.Length); for (int i = 0; i < infos.Length; i++) { Console.WriteLine("Checking {0}/{1} ...", i+1, infos.Length); MailInfo info = infos[i]; // Generate an unqiue email file name based on date time. string fileName = _generateFileName(i + 1); string fullPath = string.Format("{0}\\{1}", localInbox, fileName); Console.WriteLine("Downloading {0}/{1} ...", i + 1, infos.Length); Mail oMail = oClient.GetMail(info); // Save mail to local file oMail.SaveAs(fullPath, true); // Mark the email as deleted on server. Console.WriteLine("Deleting ... {0}/{1}", i + 1, infos.Length); oClient.Delete(info); } Console.WriteLine("Disconnecting ..."); // Delete method just mark the email as deleted, // Quit method expunge the emails from server permanently. oClient.Quit(); Console.WriteLine("Completed!"); } catch (Exception ep) { Console.WriteLine("Error: {0}", ep.Message); } } }
[C++/CLI - Retrieve email from server with event handler] using namespace System; using namespace System::Globalization; using namespace System::IO; using namespace EAGetMail; //add EAGetMail namespace void OnSendCommand( Object^ sender, array<unsigned char>^ data, bool dataStream, bool % cancel ) { if (!dataStream) { System::Text::Encoding ^encoder = System::Text::Encoding::ASCII; Console::Write(encoder->GetString(data)); } } void OnReceiveResponse( Object^ sender, array<unsigned char>^ data, bool dataStream, bool % cancel ) { if (!dataStream) { System::Text::Encoding ^encoder = System::Text::Encoding::ASCII; Console::Write(encoder->GetString(data)); } } void OnConnected(Object ^sender, System::Boolean % cancel) { Console::WriteLine("Connected"); } void OnQuit(Object ^sender, System::Boolean % cancel) { Console::WriteLine("Quit"); } void OnReceivingDataStream(Object ^sender, MailInfo ^info, int received, int total, System::Boolean % cancel) { Console::WriteLine(String::Format("Receiving {0}, {1}/{2}...", info->Index, received, total)); } void OnIdle(Object ^sender, System::Boolean % cancel) { // Application::DoEvents(); } void OnAuthorized(Object ^sender, System::Boolean % cancel) { Console::WriteLine("Authorized"); } void OnSecuring(Object ^sender, System::Boolean % cancel) { Console::WriteLine("Securing..."); } // Generate an unqiue email file name based on date time static String ^ _generateFileName(int sequence) { DateTime currentDateTime = DateTime::Now; return String::Format("{0}-{1:000}-{2:000}.eml", currentDateTime.ToString("yyyyMMddHHmmss", gcnew CultureInfo("en-US")), currentDateTime.Millisecond, sequence); } void ReceiveMail(String ^server, String ^user, String ^password, bool useSsl) { try { // Create a folder named "inbox" under current directory // to save the email retrieved. String ^localInbox = String::Format("{0}\\inbox", Directory::GetCurrentDirectory()); // If the folder is not existed, create it. if (!Directory::Exists(localInbox)) { Directory::CreateDirectory(localInbox); } // To receive email from IMAP4 server, please change // ServerProtocol.Pop3 to ServerProtocol.Imap4 in MailServer constructor // To receive email with Exchange Web Service, please change // ServerProtocol.Pop3 to ServerProtocol.ExchangeEWS to MailServer constructor // To receive email with Exchange WebDAV, please change // ServerProtocol.Pop3 to ServerProtocol.ExchangeWebDAV to MailServer constructor // Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007 // or later version, POP3/IMAP4 service is disabled by default. If you don't want to use POP3/IMAP4 // to download email from Exchange Server, you can use Exchange Web Service (Exchange 2007-2019 or // later version) or WebDAV (Exchange 2000/2003) protocol. // Most modern email server require SSL/TLS connection, // set useSsl to true is recommended. MailServer ^oServer = gcnew MailServer(server, user, password, useSsl, ServerAuthType::AuthLogin, ServerProtocol::Pop3); // POP3 port is 110, POP3 SSL port is 995 // IMAP4 port is 143, IMAP4 SSL port is 993. // EWS/WebDAV, please ignore Port property. if (oServer->Protocol == ServerProtocol::Pop3) { oServer->Port = (useSsl) ? 995 : 110; } else if (oServer->Protocol == ServerProtocol::Imap4) { oServer->Port = (useSsl) ? 993 : 143; } Console::WriteLine("Connecting server ..."); MailClient ^oClient = gcnew MailClient("TryIt"); // 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. oClient->OnAuthorized += gcnew MailClient::OnAuthorizedEventHandler(&OnAuthorized); oClient->OnConnected += gcnew MailClient::OnConnectedEventHandler(&OnConnected); oClient->OnIdle += gcnew MailClient::OnIdleEventHandler(&OnIdle); oClient->OnSecuring += gcnew MailClient::OnSecuringEventHandler(&OnSecuring); oClient->OnReceivingDataStream += gcnew MailClient::OnReceivingDataStreamEventHandler(&OnReceivingDataStream); oClient->OnQuit += gcnew MailClient::OnQuitEventHandler(&OnQuit); oClient->OnSendCommand += gcnew MailClient::OnSendCommandEventHandler(&OnSendCommand); oClient->OnReceiveResponse += gcnew MailClient::OnReceiveResponseEventHandler(&OnReceiveResponse); oClient->Connect(oServer); Console::WriteLine("Retreiving email list ..."); array<MailInfo^>^infos = oClient->GetMailInfos(); Console::WriteLine("Total {0} email(s)", infos->Length); for (int i = 0; i < infos->Length; i++) { Console::WriteLine("Checking {0}/{1}", i + 1, infos->Length); MailInfo ^info = infos[i]; // Generate an unqiue email file name based on date time String^ fileName = _generateFileName(i + 1); String^ fullPath = String::Format("{0}\\{1}", localInbox, fileName); Console::WriteLine("Downloading {0}/{1}", i + 1, infos->Length); Mail ^oMail = oClient->GetMail(info); // Save email to local disk oMail->SaveAs(fullPath, true); // Mark email as deleted on server. Console::WriteLine("Deleting ... {0}/{1}", i + 1, infos->Length); oClient->Delete(info); } Console::WriteLine("Disconnecting ..."); // Delete method just mark the email as deleted, // Quit method expunge the emails from server permanently. oClient->Quit(); Console::WriteLine("Completed!"); } catch (Exception ^ep) { Console::WriteLine("Error: {0}", ep->Message); } }