Occurs when the message is being downloaded to the destination folder.
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);
}
Imports ComponentPro.Net
Imports ComponentPro.Net.Mail
...
Shared Sub Main()
' IMAP server information.
Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "mytestpassword"
Const port As Integer = 993
Const securityMode As SslSecurityMode = SslSecurityMode.Implicit
' Create a new instance of the Imap class.
Dim client As 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.
Dim inspector As New BounceInspector()
inspector.AllowInboxDelete = False ' true if you want BounceInspector automatically delete all hard bounces.
' Handle events.
AddHandler inspector.Processing, AddressOf inspector_Processing
AddHandler inspector.Processed, AddressOf inspector_Processed
AddHandler inspector.DownloadingMessage, AddressOf inspector_DownloadingMessage
' Download messages from IMAP 'Inbox' folder to 'c:\test' and process them.
Dim result As BounceResultCollection = 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.
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)
' Disconnect.
Console.WriteLine("Disconnecting...")
client.Disconnect()
Catch imapExc As ImapException
MessageBox.Show(String.Format("An IMAP error occurred: {0}, ErrorStatus: {1}", imapExc.Message, imapExc.Status))
Catch exc As Exception
MessageBox.Show(String.Format("An error occurred: {0}", exc.Message))
End Try
End Sub
Private Shared Sub inspector_Processing(ByVal sender As Object, ByVal e As ProcessingEventArgs)
' Skip all messages containing "Test" in subject.
If e.MailMessage.Subject.IndexOf("Test") <> -1 Then
e.Skip = True
End If
End Sub
Private Shared id As Integer = 0
''' <summary>
''' Handles the BounceInspector's DownloadingMessages event.
''' </summary>
''' <param name="sender">The BounceInspector object.</param>
''' <param name="e">The event arguments.</param>
Private Shared Sub inspector_DownloadingMessage(ByVal sender As Object, ByVal e As DownloadingMessageEventArgs)
If e.FileExists Then
' Skip if message containing "Cxj89" already exists.
If e.ImapMessage.UniqueId.IndexOf("Cxj89") <> -1 Then
e.Skip = True
' Otherwise rename destination file name.
Else
e.FileName = "tmp" & id
id += 1
' You can also set e.DirectoryName to another destination folder.
' e.DirectoryName = "temp";
End If
End If
End Sub
''' <summary>
''' Handles the BounceInspector's Processed event.
''' </summary>
''' <param name="sender">The BounceInspector object.</param>
''' <param name="e">The event arguments.</param>
Private Shared Sub inspector_Processed(ByVal sender As Object, ByVal e As ProcessedEventArgs)
Console.WriteLine("Processed mail with subject '{0}'...", e.MailMessage.Subject)
End Sub
Shows how to handle the DownloadingMessage event when processing messages downloaded from a POP3 server.
using System;
using System.Windows.Forms;
using ComponentPro.Net;
using ComponentPro.Net.Mail;
...
static void Main()
{
// POP3 server information.
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "mytestpassword";
const int port = 995;
const SslSecurityMode securityMode = SslSecurityMode.Implicit;
// Create a new instance of the Imap class.
Pop3 client = new Pop3();
try
{
Console.WriteLine("Connecting POP3 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.Processed += inspector_Processed;
inspector.DownloadingMessage += inspector_DownloadingMessage;
// Download messages from POP3 server to 'c:\test' and process them.
BounceResultCollection result = inspector.ProcessMessages(client, "c:\\test");
// Download messages from POP3 server to memory stream and process them.
//BounceResultCollection result = inspector.ProcessMessages(client);
// 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 POP3 error occurred: {0}, ErrorStatus: {1}", imapExc.Message, imapExc.Status));
}
catch (Exception exc)
{
MessageBox.Show(string.Format("An error occurred: {0}", exc.Message));
}
}
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("Processing mail with subject '{0}'...", e.MailMessage.Subject);
}
Imports ComponentPro.Net
Imports ComponentPro.Net.Mail
...
Shared Sub Main()
' POP3 server information.
Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "mytestpassword"
Const port As Integer = 995
Const securityMode As SslSecurityMode = SslSecurityMode.Implicit
' Create a new instance of the Imap class.
Dim client As New Pop3()
Try
Console.WriteLine("Connecting POP3 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.
Dim inspector As New BounceInspector()
inspector.AllowInboxDelete = False ' true if you want BounceInspector automatically delete all hard bounces.
' Handle events.
AddHandler inspector.Processed, AddressOf inspector_Processed
AddHandler inspector.DownloadingMessage, AddressOf inspector_DownloadingMessage
' Download messages from POP3 server to 'c:\test' and process them.
Dim result As BounceResultCollection = inspector.ProcessMessages(client, "c:\test")
' Download messages from POP3 server to memory stream and process them.
'BounceResultCollection result = inspector.ProcessMessages(client);
' 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)
' Disconnect.
Console.WriteLine("Disconnecting...")
client.Disconnect()
Catch imapExc As ImapException
MessageBox.Show(String.Format("An POP3 error occurred: {0}, ErrorStatus: {1}", imapExc.Message, imapExc.Status))
Catch exc As Exception
MessageBox.Show(String.Format("An error occurred: {0}", exc.Message))
End Try
End Sub
Private Shared id As Integer = 0
''' <summary>
''' Handles the BounceInspector's DownloadingMessages event.
''' </summary>
''' <param name="sender">The BounceInspector object.</param>
''' <param name="e">The event arguments.</param>
Private Shared Sub inspector_DownloadingMessage(ByVal sender As Object, ByVal e As DownloadingMessageEventArgs)
If e.FileExists Then
' Skip if message containing "Cxj89" already exists.
If e.ImapMessage.UniqueId.IndexOf("Cxj89") <> -1 Then
e.Skip = True
' Otherwise rename destination file name.
Else
e.FileName = "tmp" & id
id += 1
' You can also set e.DirectoryName to another destination folder.
' e.DirectoryName = "temp";
End If
End If
End Sub
''' <summary>
''' Handles the BounceInspector's Processed event.
''' </summary>
''' <param name="sender">The BounceInspector object.</param>
''' <param name="e">The event arguments.</param>
Private Shared Sub inspector_Processed(ByVal sender As Object, ByVal e As ProcessedEventArgs)
Console.WriteLine("Processing mail with subject '{0}'...", e.MailMessage.Subject)
End Sub