|
Capturing Smtp related events |
|
The Smtp class may publish one or more events during the lifetime of an Smtp session. Any object that subscribes to events published by the Smtp class can receive and process these events.
| • | The ConnectedEvent is fired by the Smtp instance once a connection to the SMTP server has been established. |
| • | The DisconnectedEvent is fired by the Smtp instance once the connection to the SMTP server has been released. |
| • | The CommandSentEvent is fired by the Smtp instance for each command sent to the SMTP server. |
| • | The DataReceivedEvent is fired by the Smtp instance for each response received from the SMTP server. |
Capturing the ConnectedEvent event
The ConnectedEvent is published by the Smtp instance once a connection to the SMTP server has been established.
Example
This example shows you how you might subscribe to and process the ConnectedEvent event.
[C#]
mySmtp = new Smtp("hostname");
// Subscribe to event
mySmtp.ConnectedEvent += new Smtp.ConnectedEventHandler(OnConnected);
...
// invoke connect method
mySmtp.Connect();
// Define Connected Event handler
public void OnConnected(object sender, SmtpConnectedEventArgs e) {
Console.WriteLine("Connected to {0}", e.Host);
}
[Visual Basic]
Public WithEvents mySmtp As Smtp
mySmtp = New Smtp("hostname");
...
' invoke connect method
mySmtp.Connect()
' Define Connected Event handler
Private Sub OnConnected( ByVal sender As Object, ByVal e As SmtpConnectedEventArgs) Handles mySmtp.ConnectedEvent
Console.WriteLine("Connected to {0}", e.Host);
End Sub
Capturing the DisconnectedEvent event
The DisconnectedEvent is published by the Smtp instance once the connection to the SMTP server has been released.
Example
This example shows you how you might subscribe to and process the DisconnectedEvent event.
[C#]
mySmtp = new Smtp(hostname);
// Subscribe to event
mySmtp.DisconnectedEvent += new Smtp.DisconnectedEventHandler(OnDisconnected);
...
// invoke disconnect method
mySmtp.Disconnect();
// Define Disconnected Event handler
public void OnDisconnected(object sender, SmtpDisconnectedEventArgs e) {
Console.WriteLine("Disconnected");
}
[Visual Basic]
Public WithEvents mySmtp As Smtp
mySmtp = New Smtp(hostname);
...
' invoke disconnect method
mySmtp.Disconnect()
' Define Disconnected Event handler
Private Sub OnDisconnected( ByVal sender As Object, ByVal e As SmtpDisconnectedEventArgs) Handles mySmtp.DisconnectedEvent
Console.WriteLine("Disconnected");
End Sub
Capturing the CommandSentEvent event
The CommandSentEvent is published by the Smtp instance for each command sent to the SMTP server. Commands are sent to the SMTP server when the IssueCommand method or the Send method is invoked.
Example
This example displays the command sent to the Smtp server.
[C#]
mySmtp = new Smtp(hostname);
// Subscribe to event
mySmtp.CommandSentEvent += new Smtp.CommandSentEventHandler(OnCommandSent);
// Define CommandSent Event handler
public void OnCommandSent(object sender, SmtpCommandSentEventArgs e) {
Console.WriteLine("Command: " + e.Command);
}
[Visual Basic]
Public WithEvents mySmtp As Smtp
mySmtp = New Smtp(hostname);
' Define CommandSent Event handler
Private Sub OnCommandSent( ByVal sender As Object, ByVal e As SmtpCommandSentEventArgs) Handles mySmtp.CommandSentEvent
Console.WriteLine("Command: " & e.Command);
End Sub
Capturing the DataReceivedEvent event
The DataReceivedEvent is published by the Smtp instance for each response received from the SMTP server. SMTP server responses are recieved when each command is sent to the SMTP server.
Example
This example displays the Smtp server response when recieved.
[C#]
mySmtp = new Smtp(hostname);
// Subscribe to event
mySmtp.DataReceivedEvent += new Smtp.DataReceivedEventHandler(OnDataReceived);
// Define DataReceived Event handler
public void OnDataReceived(object sender, SmtpDataReceivedEventArgs e) {
Console.WriteLine("Response: " + e.Response);
}
[Visual Basic]
Public WithEvents mySmtp As Smtp
mySmtp = New Smtp(hostname);
' Define DataReceived Event handler
Private Sub OnDataReceived( ByVal sender As Object, ByVal e As SmtpDataReceivedEventArgs) Handles mySmtp.DataReceivedEvent
Console.WriteLine("Response: " & e.Response);
End Sub