SmtpClient.Connected Event


Occurs when the client has connected to SMTP server successfully.

[Visual Basic]
Public Event Connected As OnConnectedEventHandler
Public Delegate Sub OnConnectedEventHandler( _
    ByVal sender As Object, _
    ByVal e As SmtpStatusEventArgs _
)
[C#]
public event OnConnectedEventHandler Connected;
public delegate void OnConnectedEventHandler( 
     object sender, 
     SmtpStatusEventArgs e 
);
[C++]
public: __event OnConnectedEventHandler^ Connected;
public ref __delegate void OnConnectedEventHandler( 
     Object^ sender, 
     SmtpStatusEventArgs^ e 
);
[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

sender
The source (SmtpClient instance) of the event.
e
A SmtpStatusEventArgs instance indicating current status (e.Status).

Example

[Visual Basic, C#, JavaScript]The following example demonstrates how to send email in Windows 8 Store App with events usage. To get the full samples of EASendMail, please refer to Samples section.

[C# - Send Email with Events - XAML - Windows Store Apps]
using System.Threading.Tasks;
using EASendMail;
        
private void OnSecuring(
    object sender,
    SmtpStatusEventArgs e
)
{
    string status = e.Status;
    //status = "Securing ... "; 
}

private void OnAuthorized(
    object sender,
    SmtpStatusEventArgs e
)
{
    string status = e.Status;
    // status = "Authorized";
}

public void OnConnected(
    object sender,
    SmtpStatusEventArgs e
)
{
    string status = e.Status;
    // status = "Connected";
}

public void OnDisconnecting(
    object sender,
    SmtpStatusEventArgs e
)
{
    string status = e.Status;
    // status = "Connected";
}

public void OnSendingDataStream(
    object sender,
    SmtpDataStreamEventArgs e
)
{
    string status = String.Format("{0}/{1} sent", e.Sent, e.Total);
}

private async Task Send_Email_Events()
{
    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 = "test email from C# XAML project";
        oMail.TextBody = "test email with 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();

        // Add event handlers
        oSmtp.Authorized += OnAuthorized;
        oSmtp.Connected += OnConnected;
        oSmtp.Disconnecting += OnDisconnecting;
        oSmtp.Securing += OnSecuring;
        oSmtp.SendingDataStream += OnSendingDataStream;

        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 Email with Events - XAML - Windows Store Apps]
Imports System.Threading.Tasks
Imports EASendMail

Private Sub OnSecuring(sender As Object, e As SmtpStatusEventArgs)
    Dim status As String = e.Status
    ' status = "Securing ... "
End Sub

Private Sub OnAuthorized(sender As Object, e As SmtpStatusEventArgs)
    Dim status As String = e.Status
    ' status = "Authorized"
End Sub

Public Sub OnConnected(sender As Object, e As SmtpStatusEventArgs)
    Dim status As String = e.Status
    ' status = "Connected"
End Sub

Public Sub OnDisconnecting(sender As Object, e As SmtpStatusEventArgs)
    Dim status As String = e.Status
    ' status = "Disconnecting ..."
End Sub

Public Sub OnSendingDataStream(sender As Object, e As SmtpDataStreamEventArgs)
    Dim status As String = [String].Format("{0}/{1} sent", e.Sent, e.Total)
End Sub

Public Async Function Send_Email_Events() 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 = "test email from VB XAML project"
        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

        Dim oSmtp As New SmtpClient()

        ' Add event handlers
        AddHandler oSmtp.Authorized, AddressOf OnAuthorized
        AddHandler oSmtp.Connected, AddressOf OnConnected
        AddHandler oSmtp.Disconnecting, AddressOf OnDisconnecting
        AddHandler oSmtp.Securing, AddressOf OnSecuring
        AddHandler oSmtp.SendingDataStream, AddressOf OnSendingDataStream

        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 Email with Events - HTML5 - Windows Store Apps]
function send_email_events()
{
    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.add(new EASendMail.MailAddress("support@emailarchitect.net"));

    // Set email subject and body text
    oMail.subject = "test email from JavaScript HTML5 project";
    oMail.textBody = "this is a test email sent from Windows Store Apps, 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;

    var oSmtp = new EASendMail.SmtpClient();
    
    // EASendMail Events Handlers
    oSmtp.addEventListener("connected",
        (function (e) {
            var status = e.status;
            status = "Connected";
        }));

    oSmtp.addEventListener("disconnecting",
        (function (e) {
            var status = e.status;
            status = "Disconnecting ...";
        }));

    oSmtp.addEventListener("authorized",
        (function (e) {
            var status = e.status;
            status =  "Authorized";
        }));

    oSmtp.addEventListener("securing",
        (function (e) {
            var status = e.status;
            status = "Securing ...";
        }));

    oSmtp.addEventListener("sendingdatastream",
        (function (e) {
            var status = "Data " + e.sent + "/" + e.total + " sent";

        }));


    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