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 Imap 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 Imap class.
- Now you can connect to the IMAP server with Connect methods. The code looks similar to the following:
// Create a new Imap instance.
Imap client = new Imap();
// Connect to the IMAP server.
client.Connect("myserver");
' Create a new Imap instance.
Dim client As New Imap()
' Connect to the IMAP 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:
// Register an event handler.
client.Progress += client_Progress;
client.Select("INBOX");
// Download a message with sequence number #1.
client.DownloadMessage(1, "c:\\temp\\my message.eml");
' Register an event handler.
AddHandler client.Progress, AddressOf client_Progress
client.Select("INBOX")
' Download a message with sequence number #1.
client.DownloadMessage(1, "c:\temp\my message.eml")
- After completing your work, call the Disconnect method to close the IMAP session.
Final example code
using System;
using ComponentPro.Net.Mail;
...
public void ShowProgress()
{
// Create a new Imap instance.
Imap client = new Imap();
// Connect to the IMAP server.
client.Connect("myserver");
// Authenticate.
client.Authenticate("test", "test");
try
{
// Register an event handler.
client.Progress += client_Progress;
client.Select("INBOX");
// Download a message with sequence number #1.
client.DownloadMessage(1, "c:\\temp\\my message.eml");
}
catch (ImapException exc)
{
Console.WriteLine("Exception: " + exc.Message);
}
// Disconnect.
client.Disconnect();
}
void client_Progress(object sender, ImapProgressEventArgs e)
{
// Show progress information.
if (e.Length > 0 && e.State == ImapTransferState.Downloading)
{
Console.Write("\rDownloaded: {0} bytes ({1}% completed)", e.BytesTransferred, e.Percentage);
}
}
Imports ComponentPro.Net.Mail
...
Public Sub ShowProgress()
' Create a new Imap instance.
Dim client As New Imap()
' Connect to the IMAP server.
client.Connect("myserver")
' Authenticate.
client.Authenticate("test", "test")
Try
' Register an event handler.
AddHandler client.Progress, AddressOf client_Progress
client.Select("INBOX")
' Download a message with sequence number #1.
client.DownloadMessage(1, "c:\temp\my message.eml")
Catch exc As ImapException
Console.WriteLine("Exception: " & exc.Message)
End Try
' Disconnect.
client.Disconnect()
End Sub
Private Sub client_Progress(ByVal sender As Object, ByVal e As ImapProgressEventArgs)
' Show progress information.
If e.Length > 0 AndAlso e.State = ImapTransferState.Downloading Then
Console.Write(vbCr & "Downloaded: {0} bytes ({1}% completed)", e.BytesTransferred, e.Percentage)
End If
End Sub