如何检测我获取的电子邮件是否被退回
How can I detect if my fetched email is bounced or not
我使用 IMAP 或 POP3 从服务器获取电子邮件并将获取的电子邮件输入数据库,但我注意到系统中输入了很多退回的电子邮件,因此我在 google 上搜索了很多以检查获取的电子邮件,如果它是退回的电子邮件,我不会将其输入系统,我发现库 BounceDetectResult 可以检测电子邮件是否被退回,但该库仅适用于消息类型 MimeMessage,因此当我使用 IMAP 时它很有用,但它不适用于消息输入OpenPop.Mime.Message 所以我在使用POP3
时不能使用它
var result= BounceDetectorMail.Detect(message);//message type MimeMessage
if (result.IsBounce)
{
em.DelivaryFailure = true;
}
所以我的问题是当我使用 pop3 检索时,我没有找到检测检索到的消息是否被退回的方法
对于 Bounce inspector software library 可能需要同时支持 POP3 和 IMAP 的任何人:
// POP3 server information.
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "mytestpassword";
const int port = 995;
const SecurityMode securityMode = SecurityMode.Implicit;
// Create a new instance of the Pop3Client class.
Pop3Client client = new Pop3Client();
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.
// Register processed event handler.
inspector.Processed += inspector_Processed;
// Download messages from Pop3 Inbox to 'c:\test' and process them.
BounceResultCollection result = inspector.ProcessMessages(client, "c:\test");
// 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();
看起来像 MailBounceDetector library that you mentioned uses my MimeKit 库来检测邮件是否被退回。
好消息是您可以使用那个库,因为我也有一个名为MailKit的执行 POP3 的库, 所以你可以用它代替 OpenPOP.NET.
我使用 IMAP 或 POP3 从服务器获取电子邮件并将获取的电子邮件输入数据库,但我注意到系统中输入了很多退回的电子邮件,因此我在 google 上搜索了很多以检查获取的电子邮件,如果它是退回的电子邮件,我不会将其输入系统,我发现库 BounceDetectResult 可以检测电子邮件是否被退回,但该库仅适用于消息类型 MimeMessage,因此当我使用 IMAP 时它很有用,但它不适用于消息输入OpenPop.Mime.Message 所以我在使用POP3
时不能使用它 var result= BounceDetectorMail.Detect(message);//message type MimeMessage
if (result.IsBounce)
{
em.DelivaryFailure = true;
}
所以我的问题是当我使用 pop3 检索时,我没有找到检测检索到的消息是否被退回的方法
对于 Bounce inspector software library 可能需要同时支持 POP3 和 IMAP 的任何人:
// POP3 server information.
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "mytestpassword";
const int port = 995;
const SecurityMode securityMode = SecurityMode.Implicit;
// Create a new instance of the Pop3Client class.
Pop3Client client = new Pop3Client();
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.
// Register processed event handler.
inspector.Processed += inspector_Processed;
// Download messages from Pop3 Inbox to 'c:\test' and process them.
BounceResultCollection result = inspector.ProcessMessages(client, "c:\test");
// 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();
看起来像 MailBounceDetector library that you mentioned uses my MimeKit 库来检测邮件是否被退回。
好消息是您可以使用那个库,因为我也有一个名为MailKit的执行 POP3 的库, 所以你可以用它代替 OpenPOP.NET.