When there are too many mail messages to process and your application may take time to process all of them, you may wish to show the progress of the validation to the user. The UltimateBounceInspector component provides progress notification through the Progress event. The Progress event is raised periodically while e-mail validation is in progress, making accessible necessary data to display progress information, such as e-mail address and validation level.
The following steps show you how to use the Progress event to show progress information while validating e-mail addresses:
Displaying progress while validating e-mail addresses
- 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 BounceInspector class and register an event handler to the Progress event.
// Create a new instance of the BounceInspector class.
BounceInspector inspector = new BounceInspector();
// Register an event handler.
inspector.Progress += inspector_Progress;
' Create a new instance of the BounceInspector class.
Dim inspector As New BounceInspector()
' Register an event handler.
AddHandler inspector.Progress, AddressOf inspector_Progress
- Now pass the path of the directory containing EML files to scan to the ProcessMessages method. The code looks similar to the following:
// Process all EML files in directory 'c:\\temp'.
BounceResultCollection result = inspector.ProcessMessages("c:\\temp");
// 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);
' Process all EML files in directory 'c:\\temp'.
Dim result As BounceResultCollection = inspector.ProcessMessages("c:\temp")
' Display processed emails.
For Each r As BounceResult In result
' If this message was identified as a bounced email message.
If r.Identified Then
' Print out the result
Console.Write("FileName: {0}" & vbLf & "Subject: {1}" & vbLf & "Address: {2}" & vbLf & "Bounce Category: {3}" & vbLf & "Bounce Type: {4}" & vbLf & "Deleted: {5}" & vbLf & "DSN Action: {6}" & vbLf & "DSN Diagnostic Code: {7}" & vbLf & vbLf, 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)
End If
Next r
Console.WriteLine("{0} bounced message found", result.BounceCount)
Final example code
using System;
using ComponentPro.Net.Mail;
...
static void Main()
{
try
{
// Create a new instance of the BounceInspector class.
BounceInspector inspector = new BounceInspector();
// Register an event handler.
inspector.Progress += inspector_Progress;
// Process all EML files in directory 'c:\\temp'.
BounceResultCollection result = inspector.ProcessMessages("c:\\temp");
// 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);
}
catch (Exception exc)
{
Console.WriteLine(string.Format("An error occurred: {0}", exc.Message));
}
}
/// <summary>
/// Handles the BounceInspector's Progress event.
/// </summary>
/// <param name="sender">The BounceInspector object.</param>
/// <param name="e">The event arguments.</param>
static void inspector_Progress(object sender, ProgressEventArgs e)
{
Console.WriteLine("{0}/{1} message(s) processed, {2}% completed", e.Current, e.Total, e.Percentage);
}
Imports ComponentPro.Net.Mail
...
Shared Sub Main()
Try
' Create a new instance of the BounceInspector class.
Dim inspector As New BounceInspector()
' Register an event handler.
AddHandler inspector.Progress, AddressOf inspector_Progress
' Process all EML files in directory 'c:\\temp'.
Dim result As BounceResultCollection = inspector.ProcessMessages("c:\temp")
' Display processed emails.
For Each r As BounceResult In result
' If this message was identified as a bounced email message.
If r.Identified Then
' Print out the result
Console.Write("FileName: {0}" & vbLf & "Subject: {1}" & vbLf & "Address: {2}" & vbLf & "Bounce Category: {3}" & vbLf & "Bounce Type: {4}" & vbLf & "Deleted: {5}" & vbLf & "DSN Action: {6}" & vbLf & "DSN Diagnostic Code: {7}" & vbLf & vbLf, 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)
End If
Next r
Console.WriteLine("{0} bounced message found", result.BounceCount)
Catch exc As Exception
Console.WriteLine(String.Format("An error occurred: {0}", exc.Message))
End Try
End Sub
''' <summary>
''' Handles the BounceInspector's Progress event.
''' </summary>
''' <param name="sender">The BounceInspector object.</param>
''' <param name="e">The event arguments.</param>
Private Shared Sub inspector_Progress(ByVal sender As Object, ByVal e As ProgressEventArgs)
Console.WriteLine("{0}/{1} message(s) processed, {2}% completed", e.Current, e.Total, e.Percentage)
End Sub