public class DownloadingMessageEventArgs : AsyncEventArgs
Shows how to handle the DownloadingMessage event when processing messages downloaded from an IMAP server.
using System; using System.Windows.Forms; using ComponentPro.Net; using ComponentPro.Net.Mail; ... static void Main() { // IMAP server information. const string serverName = "myserver"; const string user = "name@domain.com"; const string password = "mytestpassword"; const int port = 993; const SslSecurityMode securityMode = SslSecurityMode.Implicit; // Create a new instance of the Imap class. Imap client = new Imap(); try { Console.WriteLine("Connecting IMAP server: {0}:{1}...", serverName, port); // Connect to the server. client.Connect(serverName, port, securityMode); // Login to the server. Console.WriteLine("Logging in as {0}...", user); client.Authenticate(user, password); // Initialize BounceInspector. BounceInspector inspector = new BounceInspector(); inspector.AllowInboxDelete = false; // true if you want BounceInspector automatically delete all hard bounces. // Handle events. inspector.Processing += inspector_Processing; inspector.Processed += inspector_Processed; inspector.DownloadingMessage += inspector_DownloadingMessage; // Download messages from IMAP 'Inbox' folder to 'c:\test' and process them. BounceResultCollection result = inspector.ProcessMessages(client, "Inbox", "c:\\test"); // Download messages from IMAP 'Inbox' folder to memory stream and process them. //BounceResultCollection result = inspector.ProcessMessages(client, "Inbox"); // Display processed emails. foreach (BounceResult r in result) { // If this message was identified as a bounced email message. if (r.Identified) { // Print out the result Console.Write("FileName: {0}\nSubject: {1}\nAddress: {2}\nBounce Category: {3}\nBounce Type: {4}\nDeleted: {5}\nDSN Action: {6}\nDSN Diagnostic Code: {7}\n\n", System.IO.Path.GetFileName(r.FilePath), r.MailMessage.Subject, r.Addresses[0], r.BounceCategory.Name, r.BounceType.Name, r.FileDeleted, r.Dsn.Action, r.Dsn.DiagnosticCode); } } Console.WriteLine("{0} bounced message found", result.BounceCount); // Disconnect. Console.WriteLine("Disconnecting..."); client.Disconnect(); } catch (ImapException imapExc) { MessageBox.Show(string.Format("An IMAP error occurred: {0}, ErrorStatus: {1}", imapExc.Message, imapExc.Status)); } catch (Exception exc) { MessageBox.Show(string.Format("An error occurred: {0}", exc.Message)); } } static void inspector_Processing(object sender, ProcessingEventArgs e) { // Skip all messages containing "Test" in subject. if (e.MailMessage.Subject.IndexOf("Test") != -1) e.Skip = true; } static int id = 0; /// <summary> /// Handles the BounceInspector's DownloadingMessages event. /// </summary> /// <param name="sender">The BounceInspector object.</param> /// <param name="e">The event arguments.</param> static void inspector_DownloadingMessage(object sender, DownloadingMessageEventArgs e) { if (e.FileExists) // Skip if message containing "Cxj89" already exists. if (e.ImapMessage.UniqueId.IndexOf("Cxj89") != -1) { e.Skip = true; } // Otherwise rename destination file name. else { e.FileName = "tmp" + id; id++; // You can also set e.DirectoryName to another destination folder. // e.DirectoryName = "temp"; } } /// <summary> /// Handles the BounceInspector's Processed event. /// </summary> /// <param name="sender">The BounceInspector object.</param> /// <param name="e">The event arguments.</param> static void inspector_Processed(object sender, ProcessedEventArgs e) { Console.WriteLine("Processed mail with subject '{0}'...", e.MailMessage.Subject); }