To abort any transfer (downloading or uploading message) in progress you can either call the Cancel method or simply close the connection by calling the Disconnect or DisconnectAsync method.
The following steps show how to use it.
Cancelling an operation
- Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
using System;
using ComponentPro.Net.Mail;
Imports ComponentPro.Net.Mail
- Create a new instance of the Pop3 class.
- Now you can connect to the POP3 server with Connect methods. The code looks similar to the following:
// Create a new Pop3 instance.
Pop3 client = new Pop3();
// Connect to the POP3 server.
client.Connect("myserver");
' Create a new Pop3 instance.
Dim client As New Pop3()
' Connect to the POP3 server.
client.Connect("myserver")
- Use your user name and password to login with Authenticate methods. The code looks similar to the following:
// Authenticate.
client.Authenticate("test", "test");
' Authenticate.
client.Authenticate("test", "test")
- In this example, we will demonstrate how to abort a message download when the total bytes transferred exceeds 500Kb. It can be achieved by registering an event handler to the Progress event and call the Cancel method in this method. Other scenario is creating a Cancel button on your form and call the Cancel method when user clicks on it. The event handler of the Progress event looks similar to:
void client_Progress(object sender, Pop3ProgressEventArgs e)
{
// Abort the download operation if the bytes transferred is greater than or equal to 500Kb.
if (e.State == Pop3TransferState.Downloading && e.BytesTransferred >= 1024 * 500)
{
((Pop3)sender).Cancel();
}
}
Private Sub client_Progress(ByVal sender As Object, ByVal e As Pop3ProgressEventArgs)
' Abort the download operation if the bytes transferred is greater than or equal to 500Kb.
If e.State = Pop3TransferState.Downloading AndAlso e.BytesTransferred >= 1024 * 500 Then
CType(sender, Pop3).Cancel()
End If
End Sub
- Now pass the message sequence number and destination file name to the DownloadMessage method. The code looks similar to the following:
// Register an event handler.
client.Progress += client_Progress;
' Register an event handler.
AddHandler client.Progress, AddressOf client_Progress
- After completing your work, call the Disconnect method to close the POP3 session.
Final example code
using System;
using ComponentPro.Net.Mail;
...
public void DoAbort()
{
// Create a new Pop3 instance.
Pop3 client = new Pop3();
// Connect to the POP3 server.
client.Connect("myserver");
// Authenticate.
client.Authenticate("test", "test");
try
{
// Register an event handler.
client.Progress += client_Progress;
// Download a message with sequence number #1.
client.DownloadMessage(1, "c:\\temp\\my message.eml");
}
catch (Pop3Exception exc)
{
Console.WriteLine("Exception: " + exc.Message);
}
// Disconnect.
client.Disconnect();
}
void client_Progress(object sender, Pop3ProgressEventArgs e)
{
// Abort the download operation if the bytes transferred is greater than or equal to 500Kb.
if (e.State == Pop3TransferState.Downloading && e.BytesTransferred >= 1024 * 500)
{
((Pop3)sender).Cancel();
}
}
Imports ComponentPro.Net.Mail
...
Public Sub DoAbort()
' Create a new Pop3 instance.
Dim client As New Pop3()
' Connect to the POP3 server.
client.Connect("myserver")
' Authenticate.
client.Authenticate("test", "test")
Try
' Register an event handler.
AddHandler client.Progress, AddressOf client_Progress
' Download a message with sequence number #1.
client.DownloadMessage(1, "c:\temp\my message.eml")
Catch exc As Pop3Exception
Console.WriteLine("Exception: " & exc.Message)
End Try
' Disconnect.
client.Disconnect()
End Sub
Private Sub client_Progress(ByVal sender As Object, ByVal e As Pop3ProgressEventArgs)
' Abort the download operation if the bytes transferred is greater than or equal to 500Kb.
If e.State = Pop3TransferState.Downloading AndAlso e.BytesTransferred >= 1024 * 500 Then
CType(sender, Pop3).Cancel()
End If
End Sub