When the mail message to transfer is big and your application may take time to transfer, you may wish to show the progress of the transfer to the user. The Pop3 class provides progress notification through the Progress event. The Progress event is raised periodically while data transfer is in progress, making accessible necessary data to display progress information, such as the size of the file and the number of transferred bytes.
The following steps show you how to use the Progress event to show progress information while transferring a file:
Displaying progress while transferring data
- 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")
- Register an event handler to the Progress event.
- Do your works such as downloading messages, etc. The code looks similar to the following:
try
{
// Register an event handler.
client.Progress += client_Progress;
Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.Size | Pop3EnvelopeParts.MessageInboxIndex);
// Download the first message.
_messageSize = list[0].Size;
// Download the first one.
client.DownloadMessage(list[0].MessageInboxIndex, "c:\\temp\\my message.eml");
}
catch (Pop3Exception exc)
{
Console.WriteLine("Exception: " + exc.Message);
}
Try
' Register an event handler.
AddHandler client.Progress, AddressOf client_Progress
Dim list As Pop3MessageCollection = client.ListMessages(Pop3EnvelopeParts.Size Or Pop3EnvelopeParts.MessageInboxIndex)
' Download the first message.
_messageSize = list(0).Size
' Download the first one.
client.DownloadMessage(list(0).MessageInboxIndex, "c:\temp\my message.eml")
Catch exc As Pop3Exception
Console.WriteLine("Exception: " & exc.Message)
End Try
- After completing your work, call the Disconnect method to close the POP3 session.
Final example code
using System;
using ComponentPro.Net.Mail;
...
private long _messageSize;
public void ShowProgress()
{
// 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;
Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.Size | Pop3EnvelopeParts.MessageInboxIndex);
// Download the first message.
_messageSize = list[0].Size;
// Download the first one.
client.DownloadMessage(list[0].MessageInboxIndex, "c:\\temp\\my message.eml");
}
catch (Pop3Exception exc)
{
Console.WriteLine("Exception: " + exc.Message);
}
// Disconnect.
client.Disconnect();
}
void client_Progress(object sender, Pop3ProgressEventArgs e)
{
// Show progress information.
if (e.State == Pop3TransferState.Downloading)
{
Console.Write("\rDownloaded: {0} bytes ({1}% completed)", e.BytesTransferred, e.CalculatePercentage(_messageSize));
}
}
Imports ComponentPro.Net.Mail
...
Private _messageSize As Long
Public Sub ShowProgress()
' 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
Dim list As Pop3MessageCollection = client.ListMessages(Pop3EnvelopeParts.Size Or Pop3EnvelopeParts.MessageInboxIndex)
' Download the first message.
_messageSize = list(0).Size
' Download the first one.
client.DownloadMessage(list(0).MessageInboxIndex, "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)
' Show progress information.
If e.State = Pop3TransferState.Downloading Then
Console.Write(vbCr & "Downloaded: {0} bytes ({1}% completed)", e.BytesTransferred, e.CalculatePercentage(_messageSize))
End If
End Sub