SmtpClient.BeginSendMail Method


Sends email message asynchronously.

[Visual Basic]
Public Function BeginSendMail( _
    server As SmtpServer, _
    mail As SmtpMail, _
    callback As AsyncCallback, _
    state As Object _
) As SmtpClientAsyncResult
[C#]
public SmtpClientAsyncResult BeginSendMail(
    SmtpServer server,
    SmtpMail mail,
    AsyncCallback callback,
    Object state
);
[C++]
public: SmtpClientAsyncResult^ BeginSendMail(
    SmtpServer^ server,
    SmtpMail^ mail,
    AsyncCallback^ callback,
    Object^ state
);
[JScript]
public function BeginSendMail( 
    server : SmtpServer, 
    mail : SmtpMail, 
    callback : AsyncCallback, 
    state As Object 
) : SmtpClientAsyncResult;

Parameters

server
A SmtpServer instances used to send email.
mail
A SmtpMail instance to send.
callback
The AsyncCallback delegate.
state
An object containing state information for this request.

Return Value

An SmtpClientAsyncResult that references the asynchronous send.

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

    Try
        Dim oMail As SmtpMail = New SmtpMail("TryIt")

        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")
        ' SMTP user authentication
        oServer.User = "myusername"
        oServer.Password = "mypassword"

        ' Most mordern SMTP servers require SSL/TLS connection now.
        ' ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically.
        oServer.ConnectType = SmtpConnectType.ConnectTryTLS

        Dim oSmtp As SmtpClient = New SmtpClient()
        Dim asyncResult As SmtpClientAsyncResult = oSmtp.BeginSendMail(oServer, oMail, Nothing, Nothing)
        Do While Not asyncResult.AsyncWaitHandle.WaitOne(5, False)
            ' do something
        Loop

        oSmtp.EndSendMail(asyncResult)
        Console.WriteLine("This message has been submitted to server successfully!")
    Catch exp As Exception
        Console.WriteLine("Exception: {0}", exp.Message)
    End Try

End Sub


[C# - Send Email Asynchronously] using System; using EASendMail; void SendMail() { try { SmtpMail oMail = new SmtpMail("TryIt"); 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"); // SMTP user authentication oServer.User = "myusername"; oServer.Password = "mypassword"; // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer.ConnectType = SmtpConnectType.ConnectTryTLS; SmtpClient oSmtp = new SmtpClient(); SmtpClientAsyncResult asyncResult = oSmtp.BeginSendMail(oServer, oMail, null, null); while (!asyncResult.AsyncWaitHandle.WaitOne(5, false)) { // do something } oSmtp.EndSendMail(asyncResult); Console.WriteLine("the message was sent to {0} successfully", asyncResult.SmtpClientInstance.CurrentSmtpServer.Server); } catch (Exception exp) { Console.WriteLine("Exception: {0}", exp.Message); } }
[C++/CLI - Send Email Asynchronously] using namespace System; using namespace EASendMail; void SendMail() { try { SmtpMail ^oMail = gcnew SmtpMail("TryIt"); 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"); // SMTP user authentication oServer->User = "myusername"; oServer->Password = "mypassword"; // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer->ConnectType = SmtpConnectType::ConnectTryTLS; SmtpClient ^oSmtp = gcnew SmtpClient(); SmtpClientAsyncResult^ asyncResult = oSmtp->BeginSendMail(oServer, oMail, nullptr, nullptr); while (!asyncResult->AsyncWaitHandle->WaitOne(5, false)) { // do other thing; } oSmtp->EndSendMail(asyncResult); Console::WriteLine("the message was sent to {0} successfully", asyncResult->SmtpClientInstance->CurrentSmtpServer->Server); } catch (Exception ^exp) { Console::WriteLine("Exception: {0}", exp->Message); } }

See Also

SmtpClient.EndSendMail 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