SmtpClientAsyncResult Class


Provides an implementation of System.IAsyncResult for use by BeginSendMail method and BeginTestRecipients method to implement the standard asynchronous method pattern.

System.Object
    EASendMail.SmtpClientAsyncResult

[Visual Basic]
Public Class SmtpClientAsyncResult
    Implements IAsyncResult
[C#]
public class SmtpClientAsyncResult : IAsyncResult
[C++]
public ref class SmtpClientAsyncResult : public IAsyncResult
[JScript]
public class SmtpClientAsyncResult implements IAsyncResult

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Methods and Properties

Please refer to .NET Framework SDK System.IAsyncResult interface.

Example

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

[VB - Send Email Asynchronously]
Imports EASendMail
Sub SendMail()
    Dim oMail As SmtpMail = New SmtpMail("TryIt")
    Dim oSmtp As SmtpClient = New SmtpClient
    Dim ar As SmtpClientAsyncResult = Nothing

    Try
        oMail.From = New MailAddress("from@adminsystem.com")
        oMail.To.Add(New MailAddress("to@adminsystem.com"))
        
        oMail.Subject = "test subject"
        oMail.TextBody = "test body"
        Dim oServer As SmtpServer = New SmtpServer("myserveraddress")
        ar = oSmtp.BeginSendMail(oServer, oMail, Nothing, Nothing)
        Do While Not ar.AsyncWaitHandle.WaitOne(5, False)
            'do other thing
        Loop

        oSmtp.EndSendMail(ar)

        Console.WriteLine("the message was sent to {0} successfully", _
ar.SmtpClientInstance.CurrentSmtpServer.Server)
    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

    Console.WriteLine("SMTP LOG:" & vbCrLf)
    Console.WriteLine(ar.SmtpClientInstance.SmtpConversation)
End Sub

[C# - Send Email Asynchronously]
using System;
using EASendMail;
void SendMail()
{
    SmtpMail oMail = new SmtpMail("TryIt");
    SmtpClient oSmtp = new SmtpClient();
    SmtpClientAsyncResult ar = null;
    try
    {
        oMail.From = new MailAddress("from@adminsystem.com" );
        oMail.To.Add( new MailAddress("to@adminsystem.com" ));
        oMail.Subject = "test subject";
        oMail.TextBody = "test body";
        SmtpServer oServer = new SmtpServer("myserveraddress");

        ar = oSmtp.BeginSendMail( oServer, oMail, null, null );
        while(!ar.AsyncWaitHandle.WaitOne(5, false ))
        {
            //do other thing
        }

        oSmtp.EndSendMail( ar );

        Console.WriteLine( "the message was sent to {0} successfully",
            ar.SmtpClientInstance.CurrentSmtpServer.Server );
    }
    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 );         
    }

    Console.WriteLine( "SMTP LOG:\r\n" );
    Console.WriteLine( ar.SmtpClientInstance.SmtpConversation );
}
        
[C++/CLI - Send Email Asynchronously]
using namespace System;
using namespace EASendMail;

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

    try
    {
        oMail->From = gcnew MailAddress("from@adminsystem.com" );
        oMail->To->Add( gcnew MailAddress("to@adminsystem.com" ));
        oMail->Subject = "test subject";
        oMail->TextBody = "test body";
        
        SmtpServer ^oServer = gcnew SmtpServer("myserveraddres");

        ar = oSmtp->BeginSendMail( oServer, oMail, NULL, NULL );
        while(!ar->AsyncWaitHandle->WaitOne( 5, false ))
        {
            //do other thing;
        }

        oSmtp->EndSendMail( ar );
        Console::WriteLine( "the message was sent to {0} successfully",
            ar->SmtpClientInstance->CurrentSmtpServer->Server );
    
    }
    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 );           
    }

    Console::WriteLine( "SMTP LOG:\r\n" );
    Console::WriteLine( ar->SmtpClientInstance->SmtpConversation );
}

See Also

SmtpClient.BeginSendMail Method
SmtpClient.BeginSendRawMail Method
Work with EASendMail Service (Email Queuing)
SmtpClient.SendMailToQueue Method
SmtpClient.SendMailToQueueEx Method

Online Tutorials

Send Email in VB 6.0 - Tutorial
Send Email in C# - Tutorial
Send Email in VB.NET - Tutorial
Send Email in Visual C++ - Tutorial
Send Email in Managed C++/CLI - Tutorial
Send Email in Delphi - Tutorial
Send Email in MS SQL stored procedure - Tutorial