SmtpClient.QuitAsync Method


Disconnects the smtp server asynchronously (async, await).

[Visual Basic]
Public Function QuitAsync( _
) As Task
[C#]
public Task QuitAsync(
);
[C++]
public: Task^ QuitAsync(
);
[JScript]
public function QuitAsync(
): Task;

Remarks

The combination of ConnectAsync, QuitAsync, ResetAsync methods and SmtpClient.SendMailAsync(SmtpMail mail) methods sends multiple emails in one SMTP connection session. DO NOT use ConnectAsync, QuitAsync and ResetAsync methods with SmtpClient.SendMailAsync(SmtpServer server, SmtpMail mail) method. SmtpClient.SendMailAsync(SmtpServer server, SmtpMail mail) method connects and disconnects smtp server automatically.

Example

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

[VB - Send multiple emails in one SMTP connection session]

Imports EASendMail

Async Sub SendTwoMailsInOneConnection()

    Try
        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
        Await oSmtp.ConnectAsync(oServer)

        Dim oMail As SmtpMail = New SmtpMail("TryIt")
        oMail.From = New MailAddress("from@adminsystem.com")
        oMail.To.Add(New MailAddress("to@adminsystem.com"))

        oMail.Subject = "firset test email"
        oMail.TextBody = "test body"

        Await oSmtp.SendMailAsync(oMail)
        Console.WriteLine("first sent")

        Await oSmtp.ResetAsync()

        oMail.Reset()

        oMail.From = New MailAddress("from@adminsystem.com")
        oMail.To.Add(New MailAddress("another@adminsystem.com"))

        oMail.Subject = "second test email"
        oMail.TextBody = "another body"

        Await oSmtp.SendMailAsync(oMail)
        Console.WriteLine("second sent")

        Await oSmtp.QuitAsync()

    Catch exp As Exception
        Console.WriteLine("Exception: {0}", exp.Message)
    End Try

End Sub

Async Sub SendMail()

    Try
        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 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 oSmtp As SmtpClient = New SmtpClient()

        Await oSmtp.SendMailAsync(oServer, oMail)
        Console.WriteLine("message was sent")
    Catch exp As Exception
        Console.WriteLine("Exception: {0}", exp.Message)
    End Try

End Sub


[C# - Send multiple emails in one SMTP connection session] using System; using EASendMail; async void SendTwoMailsInOneConnection() { try { 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(); await oSmtp.ConnectAsync(oServer); SmtpMail oMail = new SmtpMail("TryIt"); oMail.From = new MailAddress("from@adminsystem.com"); oMail.To.Add(new MailAddress("to@adminsystem.com")); oMail.Subject = "first test email"; oMail.TextBody = "test body"; await oSmtp.SendMailAsync(oMail); Console.WriteLine("first sent"); await oSmtp.ResetAsync(); oMail.Reset(); oMail.From = new MailAddress("from@adminsystem.com"); oMail.To.Add(new MailAddress("another@adminsystem.com")); oMail.Subject = "second test email"; oMail.TextBody = "another body"; await oSmtp.SendMailAsync(oMail); Console.WriteLine("second sent"); await oSmtp.QuitAsync(); } catch (Exception exp) { Console.WriteLine("Exception: {0}", exp.Message); } } async void SendMail() { try { 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; 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"; SmtpClient oSmtp = new SmtpClient(); await oSmtp.SendMailAsync(oServer, oMail); Console.WriteLine("message was sent"); } catch (Exception exp) { Console.WriteLine("Exception: {0}", exp.Message); } }

See Also

SmtpClient.SendMailAsync Method
SmtpClient.ConnectAsync Method
SmtpClient.ResetAsync Method