SmtpClient.ConnectAsync Method


Connects a SMTP server.

[Visual Basic]
Public Function ConnectAsync( _
    server As SmtpServer _
) As IAsyncAction
[C#]
public IAsyncAction ConnectAsync(
    SmtpServer server
);
[C++]
public: IAsyncAction^ ConnectAsync(
    SmtpServer^ server
);
[JScript]
public function ConnectAsync(
    server : SmtpServer
) : IAsyncAction;

Parameters

server
A SmtpServer instances to connect.

Return Value

An IAsyncAction interface instance to represents an asynchronous action, you can use this return object to cancel current operation.

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#, JavaScript] To get the full samples of EASendMail, please refer to Samples section.

[C# - Send multiple emails in one SMTP connection session - XAML]
using EASendMail;
using System.Threading.Tasks;

private async Task SendTwoMailsInOneConnection()
{
    String Result = "";
    try
    {
        SmtpMail oMail = new SmtpMail("TryIt");
               
        // Set sender email address, please change it to yours
        oMail.From = new MailAddress("test@emailarchitect.net");

        // Set recipient email address, please change it to yours
        oMail.To.Add(new MailAddress("support@emailarchitect.net"));

        // Set email subject and body text
        oMail.Subject = "first test email from C# XAML project";
        oMail.TextBody = "first test email sent from Windows Store App, do not reply";

        // Your SMTP server address
        SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");

        // User and password for ESMTP authentication           
        oServer.User = "test@emailarchitect.net";
        oServer.Password = "testpassword";

        // If your SMTP server requires TLS connection on 25 port, please add this line
        // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

        // If your SMTP server requires SSL connection on 465 port, please add this line
        // oServer.Port = 465;
        // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

        SmtpClient oSmtp = new SmtpClient();
        // Connect SMTP server.
        await oSmtp.ConnectAsync(oServer);
        // Send email message.
        await oSmtp.SendMailAsync(oMail);
        // Keep Connection and reset SMTP status 
        await oSmtp.ResetAsync();

        // Clear current recipient, subject, body, attachment
        oMail.Reset();

        // Set second email
        oMail.From = new MailAddress("test@emailarchitect.net");
        oMail.To.Add(new MailAddress("supportex@emailarchitect.net"));
        oMail.Subject = "second test email from C# XAML project";
        oMail.TextBody = "second test email sent from Windows Store App, do not reply";
        
        // Send second email message.        
        await oSmtp.SendMailAsync(oMail);
        // disconnect SMTP server
        await oSmtp.QuitAsync();

        Result = "Email was sent successfully!";
    }
    catch (Exception ep)
    {
        Result = String.Format("Failed to send email with the following error: {0}", ep.Message);
    }

    // Display Result by Diaglog box
    Windows.UI.Popups.MessageDialog dlg = new
        Windows.UI.Popups.MessageDialog(Result);

    await dlg.ShowAsync();
}

[C# - Send single email]
using EASendMail;
using System.Threading.Tasks;

private async Task SendEmail()
{
    String Result = "";
    try
    {
        SmtpMail oMail = new SmtpMail("TryIt");
        SmtpClient oSmtp = new SmtpClient();

        // Set sender email address, please change it to yours 
        oMail.From = new MailAddress("test@emailarchitect.net");

        // Add recipient email address, please change it to yours
        oMail.To.Add(new MailAddress("support@emailarchitect.net"));

       // Add more recipient email address
       // oMail.To.Add(new MailAddress("supportex@emailarchitect.net"));

       // Add CC recipient email address
       // oMail.Cc.Add(new MailAddress("cc@emailarchitect.net"));

       // Add BCC recipient email address
       // oMail.Bcc.Add(new MailAddress("cc@emailarchitect.net"));

        // Set email subject
        oMail.Subject = "test email from C# XAML project";

        // Set email body
        oMail.TextBody = "this is a test email sent from Windows Store App, do not reply";

        // Your SMTP server address
        SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");

        // User and password for ESMTP authentication            
        oServer.User = "test@emailarchitect.net";
        oServer.Password = "testpassword";

        // If your SMTP server requires TLS connection on 25 port, please add this line
        // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

        // If your SMTP server requires SSL connection on 465 port, please add this line
        // oServer.Port = 465;
        // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

        await oSmtp.SendMailAsync(oServer, oMail);
        Result = "Email was sent successfully!";
    }
    catch (Exception ep)
    {
        Result = String.Format("Failed to send email with the following error: {0}", ep.Message);
    }

    // Display Result by Diaglog box
    Windows.UI.Popups.MessageDialog dlg = new
        Windows.UI.Popups.MessageDialog(Result);
           
    await dlg.ShowAsync();
}


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

Private Async Function SendTwoMailsInOneConnection() As Task
    Dim Result As String = ""
    Try

        Dim oMail As New SmtpMail("TryIt")

        ' Set sender email address, please change it to yours
        oMail.From = New MailAddress("test@emailarchitect.net")

        ' Set recipient email address, please change it to yours
        oMail.To.Add(New MailAddress("support@emailarchitect.net"))

        ' Set email subject and body text
        oMail.Subject = "first test email from VB XAML project"
        oMail.TextBody = "first test email sent from Windows Store App, do not reply"

        ' Your SMTP server address
        Dim oServer As New SmtpServer("localhost")

        ' User and password for ESMTP authentication           
        oServer.User = "test@emailarchitect.net"
        oServer.Password = "testpassword"

        ' If your SMTP server requires TLS connection on 25 port, please add this line
        ' oServer.ConnectType = SmtpConnectType.ConnectSSLAuto

        ' If your SMTP server requires SSL connection on 465 port, please add this line
        ' oServer.Port = 465
        ' oServer.ConnectType = SmtpConnectType.ConnectSSLAuto

        Dim oSmtp As New SmtpClient()
        ' Connect SMTP server.
        Await oSmtp.ConnectAsync(oServer)
        ' Send email message.
        Await oSmtp.SendMailAsync(oMail)
        ' Keep Connection and reset SMTP status
        Await oSmtp.ResetAsync()

        ' Clear current recipient, subject, body, attachment
        oMail.Reset()

        ' Set second email
        oMail.From = New MailAddress("test@emailarchitect.net")
        oMail.To.Add(New MailAddress("supportex@emailarchitect.net"))
        oMail.Subject = "second test email from VB XAML project"
        oMail.TextBody = "second test email sent from Windows Store App, do not reply"

        ' Send second email message.        
        Await oSmtp.SendMailAsync(oMail)
        ' disconnect SMTP server
        Await oSmtp.QuitAsync()

        Result = "Email was sent successfully!"

    Catch ep As Exception

        Result = String.Format("Failed to send email with the following error: {0}", ep.Message)
    End Try

    ' Display Result by Diaglog box
    Dim dlg As New Windows.UI.Popups.MessageDialog(Result)
    Await dlg.ShowAsync()

End Function
        
[VB - Send single email - XAML]
Imports EASendMail

Private Async Function SendEmail() As Task
    Dim Result As String = ""
    Try

        Dim oMail As New SmtpMail("TryIt")
        Dim oSmtp As New SmtpClient()

        ' Set sender email address, please change it to yours
        oMail.From = New MailAddress("test@emailarchitect.net")

        ' Add recipient email address, please change it to yours
        oMail.To.Add(New MailAddress("support@emailarchitect.net"))

       ' Add more recipient email address
       ' oMail.To.Add(New MailAddress("supportex@emailarchitect.net"))

       ' Add CC recipient email address
       ' oMail.Cc.Add(New MailAddress("cc@emailarchitect.net"))

       ' Add BCC recipient email address
       ' oMail.Bcc.Add(New MailAddress("cc@emailarchitect.net"))

       ' Set email subject
        oMail.Subject = "test email from VB XAML project"

        ' Set email body
        oMail.TextBody = "this is a test email sent from Windows Store App, do not reply"

        ' Your SMTP server address
        Dim oServer As New SmtpServer("smtp.emailarchitect.net")

        ' User and password for ESMTP authentication            
        oServer.User = "test@emailarchitect.net"
        oServer.Password = "testpassword"

        ' If your SMTP server requires TLS connection on 25 port, please add this line
        ' oServer.ConnectType = SmtpConnectType.ConnectSSLAuto

        ' If your SMTP server requires SSL connection on 465 port, please add this line
        ' oServer.Port = 465
        ' oServer.ConnectType = SmtpConnectType.ConnectSSLAuto

        Await oSmtp.SendMailAsync(oServer, oMail)
        Result = "Email was sent successfully!"

    Catch ep As Exception
        Result = String.Format("Failed to send email with the following error: {0}", ep.Message)
    End Try

    ' Display Result by Diaglog box
    Dim dlg As New Windows.UI.Popups.MessageDialog(Result)
    Await dlg.ShowAsync()
End Function

[JavaScript - Send multiple emails in one SMTP connection session - HTML5]

function sendTwoMailsInOneConnection(oMail)
{
    var result = "";
    var oMail = new EASendMail.SmtpMail("TryIt");

    // Set sender email address, please change it to yours 
    oMail.from = new EASendMail.MailAddress("test@emailarchitect.net");

    // Add recipient email address, please change it to yours
    oMail.to.append(new EASendMail.MailAddress("ivan@emailarchitect.com"));

    // Set email subject and body text
    oMail.subject = "first test email from JavaScript HTML5 project";
    oMail.textBody = "this is a test email sent from Windows Store Apps, do not reply";

    var oSmtp = new EASendMail.SmtpClient();

    // Your SMTP server address
    var oServer = new EASendMail.SmtpServer("localhost");

    // User and password for ESMTP authentication            
    oServer.user = "test@emailarchitect.net";
    oServer.password = "testpassword";

    // If your SMTP server requires TLS connection on 25 port, please add this line
    // oServer.connectType = SmtpConnectType.connectSSLAuto;

    // If your SMTP server requires SSL connection on 465 port, please add this line
    // oServer.port = 465;
    // oServer.connectType = SmtpConnectType.connectSSLAuto;

    oSmtp.connectAsync(oServer)
        .then(function () {
            // send first email.
            return oSmtp.sendMailAsync(oMail);
        })
        .then(function () {
            // keep connection and reset SMTP status
            return oSmtp.resetAsync();
        })
        .then(function () {
            // Clear current recipient, subject, body
            oMail.reset();

            // Set second email.
            oMail.from = new EASendMail.MailAddress("test@emailarchitect.net");
            oMail.to.append(new EASendMail.MailAddress("ivan@emailarchitect.com"));
            oMail.subject = "second test email from JavaScript HTML5 project";
            oMail.textBody = "this is a test email sent from Windows Store Apps, do not reply";

            // send second email.
            return oSmtp.sendMailAsync(oMail);
        })
        .then(function () {
            // Disconnect SMTP server
            return oSmtp.quitAsync();
        })
        .done(function () { 
            result = "Email was sent successfully!";
            (new Windows.UI.Popups.MessageDialog(result, "Success")).showAsync();
        },  

        function (e) {
            // because javascript exception only gives the stack trace messages, but it is not
            // real description of exception, so we give a property lastErrorMessage for javascript.
            if (oSmtp.lastErrorMessage != "") {
                result = oSmtp.lastErrorMessage;
            }
            else {
                result = e.message;
            }

            (new Windows.UI.Popups.MessageDialog(result, "Error Information")).showAsync();
               
        });
}

[JavaScript - Send single email - HTML5]
function sendMail() {
    var result = "";

    var oMail = new EASendMail.SmtpMail("TryIt");
    var oSmtp = new EASendMail.SmtpClient();

    // Set sender email address, please change it to yours 
    oMail.from = new EASendMail.MailAddress("test@emailarchitect.net");

    // Add recipient email address, please change it to yours
    oMail.to.add(new EASendMail.MailAddress("support@emailarchitect.net"));

    // Add more recipient email address
    // oMail.to.add(new EASendMail.MailAddress("supportex@emailarchitect.net"));

    // Add CC recipient email address
    // oMail.cc.add(new EASendMail.MailAddress("cc@emailarchitect.net"));

    // Add BCC recipient email address
    // oMail.bcc.add(new EASendMail.MailAddress("cc@emailarchitect.net"));

    // Set email subject
    oMail.subject = "test email from JavaScript HTML5 project";

    // Set email body
    oMail.textBody = "this is a test email sent from Windows Store App, do not reply";

    // Your SMTP server address
    var oServer = new EASendMail.SmtpServer("smtp.emailarchitect.net");

    // User and password for ESMTP authentication            
    oServer.user = "test@emailarchitect.net";
    oServer.password = "testpassword";

    // If your SMTP server requires TLS connection on 25 port, please add this line
    // oServer.connectType = EASendMail.SmtpConnectType.connectSSLAuto;

    // If your SMTP server requires SSL connection on 465 port, please add this line
    // oServer.port = 465;
    // oServer.connectType = EASendMail.SmtpConnectType.connectSSLAuto;

    oSmtp.sendMailAsync(oServer, oMail).then(function (e) {
        result = "Email was sent successfully!";

        // Display Result by Diaglog box
        (new Windows.UI.Popups.MessageDialog(result, "Success")).showAsync();
    },

    function (e) {
        // because javascript exception only gives the stack trace messages, but it is not
        // real description of exception, so we give a property lastErrorMessage for javascript.
        if (oSmtp.lastErrorMessage != "") {
            result = oSmtp.lastErrorMessage;
        }
        else {
            result = e.message;
        }
        oSmtp.close();

        // Display Result by Diaglog box
        (new Windows.UI.Popups.MessageDialog(result, "Error Information")).showAsync();
    });
}

See Also

SmtpClient.SendMailAsync Method
SmtpClient.QuitAsync Method
SmtpClient.ResetAsync Method