MailBee.NET Objects是一款为创建、发送、接收以及处理电子邮件而设计的健壮、功能丰富的.NET控件。几行代码便可为应用程序添加E-Mail支持,
简单高效。具备“必需”以及独特的功能,这些控件帮助开发人员简单快速地将复杂的电子邮件功能添加到他们的应用程序中。
本文主要介绍了SMTP服务器退回邮件的代码示例。目前MailBee.NET Objects在线订购享75折优惠正在进行中,欢迎您下载试用版进行运用!
在上期的SMTP部分中,我们将退回的电子邮件发送到指定的单个电子邮件地址。
每封退回的电子邮件都包含失败的电子邮件地址(以及其他信息)。这个失败的地址是我们想知道的。这个任务看起来很简单 - 你需要做的是扫描退回的
邮件以确定地址的典型字段。但是有一个隐藏的危险:每个邮件服务器都有自己的邮件退回格式。虽然它们几乎具有相同的“From”和“主题”字段,但是退回地址可以在邮件的任何位置。MailEnable服务器的典型退回邮件如下所示:
From: POSTMASTER@domain.com To: jdoe@domain.com Subject: Message Delivery Failure MailEnable: Message Delivery Failure. The following recipient(s) could not be reached: Recipient: [SMTP: bill@domain.com] Reason: The message could not be delivered because the domain name (domain.com) does not appear to be registered.
函数GetInvalidEmailAddressME()检查邮件是否被退回,并从退回的邮件中提取失败的地址。由于退回邮件格式不同,此功能仅对MailEnable服务器有效。例如,如果你正在使用Communigate Pro服务器,则退回消息如下所示:
From: MAILER-DAEMON@domain.local To: jdoe@localhost Subject: Undeliverable mail: Failed to deliver to ''
正如你所看到的,这个退回的邮件与前一个几乎相同。使用哪个服务器并不重要,退回信息总是有From,To和Subject字段,退回信息在其正文中有失败的地址。函数GetInvalidEmailAddressCP()与前一个函数几乎相同; 区别仅在于检查邮件字段并搜索失败的地址。支持尽可能多的退回电子邮件格式至关重要。这就是为什么你需要修改GetInvalidEmailAddress()函数,以适应服务器使用的退回电子邮件格式。
注意:在某些情况下,你可能需要扫描退回的电子邮件以获取其他信息。如果由于暂时的问题,退回的邮件到达,则不要删除电子邮件地址:邮箱超过配
额,邮件服务器关闭等。退回邮件可以分为两种类型:硬邮件和软邮件。如果是不存在的帐户或域,则会发生硬性退回。其他故障,如完整的邮箱或暂时
不可用的域,则是软性退回。你可以为数据库中的每个地址进行计算。也许下一次尝试发送邮件到具有一些软性退回的地址时将成功。
代码示例:
在此示例中,我们从指定的帐户中检索退回的电子邮件,然后从每个退回的电子邮件中提取失败的电子邮件地址。失败的地址传递给RemoveEmailFromDatabase子程序,该子程序必须从数据库中删除失败的地址。应用程序还会从服务器中删除退回的电子邮件,以避免下次处理。
C#:
// Create POP3 object Pop3 pop = new Pop3(); // Enable logging to file pop.Log.Enabled = true; pop.Log.Filename = @"C:\log.txt"; pop.Log.Clear(); // Connect to POP3 server pop.Connect("mail.domain.com"); pop.Login("bounce", "secret"); // Download headers and bodies of all messages. MailMessageCollection msgs = pop.DownloadMessageHeaders(1, -1, -1); // Loop through all messages in the mailbox foreach (MailMessage msg in msgs) { string strLine = msg.BodyPlainText; Console.WriteLine("From: " + msg.From.Email); // Get failed email address string str_invalid_email = GetInvalidEmailAddressME(msg); // If str_invalid_email is non-empty then failed email // address was found if (str_invalid_email.Length > 0) { // Remove failed email from database RemoveEmailFromDatabase(str_invalid_email); // Display invalid adress Console.WriteLine("Invalid email: " + str_invalid_email); // Delete bounced email from server to avoid // processing it next time pop.DeleteMessage(msg.IndexOnServer); } } // Disconnect from POP3 server pop.Disconnect(); // The function checks whether the message is bounced and extracts // failed address // from bounced message. Valid only for MailEnable servers static string GetInvalidEmailAddressME(MailMessage msg) { string str_invalid_email = msg.BodyPlainText; // Check if this is a bounced message report if (msg.Subject.IndexOf("Delivery Failure") == -1) { return ""; } if (msg.From.ToString().IndexOf("POSTMASTER") == -1) { return ""; } // Now we're sure this is a bounced message report int i_start; i_start = str_invalid_email.IndexOf("SMTP:"); // Check if bounced message report contains "Recipient:" field if (i_start == -1) { return ""; } // Get failed address i_start += 5; i_end = str_invalid_email.IndexOf("]",i_start); str_invalid_email.Substring(i_start, i_end); return str_invalid_email; } // The function checks whether the message is bounced and extracts // failed address // from bounced message. Valid only for Communigate Pro servers static string GetInvalidEmailAddressCP(MailMessage msg) { string str_invalid_email = msg.BodyPlainText; // Check if this is a bounced message report if (msg.Subject.IndexOf("Undeliverable mail") == -1) { return ""; } if (msg.From.ToString().IndexOf("MAILER-DAEMON") == -1) { return ""; } // Now we're sure this is a bounced message report int i_start; i_start = str_invalid_email.IndexOf("to '<"); // Check if bounced message report contains // "Failed to deliver to " field if (i_start == -1) { return ""; } // Get failed address i_start += 5; i_end = str_invalid_email.IndexOf("]",i_start); str_invalid_email.Substring(i_start, i_end); return str_invalid_email; } // This function must remove (or disable) specified // email address from mailing list static void RemoveEmailFromDatabase(string str_invalid_email) { // TODO: Add your code here }
VB.NET:
Dim pop As New Pop3 ' Enable logging to file pop.Log.Enabled = True pop.Log.Filename = "C:\log.txt" pop.Log.Clear() ' Connect to POP3 server pop.Connect("mail.domain.com") pop.Login("jdoe", "secret") ' Download headers and bodies for all messages. Dim msgs As MailMessageCollection = pop.DownloadMessageHeaders(1, -1, -1) ' Loop through all messages in the mailbox Dim msg As MailMessage For Each msg In msgs Dim strLine As String = msg.BodyPlainText Console.WriteLine("From: " + msg.From.Email) ' Get failed email address Dim str_invalid_email As String = GetInvalidEmailAddressME(msg) ' If str_invalid_email is non-empty then failed email ' address was found If str_invalid_email.Length > 0 Then 'Remove failed email from database RemoveEmailFromDatabase(str_invalid_email) ' Display invalid address Console.WriteLine("Invalid email: " & str_invalid_email) ' Delete bounced email from server to avoid ' processing it next time pop.DeleteMessage(msg.IndexOnServer) End If Next Console.ReadLine() ' Disconnect from POP3 server pop.Disconnect() ' The function checks whether the message is bounced and extracts ' failed address ' from bounced message. Valid only for MailEnable servers Function GetInvalidEmailAddressME(ByVal msg As MailMessage) As String Dim str_invalid_email As String = msg.BodyPlainText ' Check if this is a bounced message report If msg.Subject.IndexOf("Delivery Failure") = -1 Then Return "" End If If msg.From.ToString().IndexOf("POSTMASTER") = -1 Then Return "" End If ' Now we're sure this is a bounced message report Dim i_start As Integer, i_end As Integer i_start = str_invalid_email.IndexOf("SMTP:") ' Check if bounced message report contains "Recipient:" field If i_start = -1 Then Return "" End If ' Get failed address i_start += 5 i_end = str_invalid_email.IndexOf("]", i_start) str_invalid_email.Substring(i_start, i_end) Return str_invalid_email End Function ' The function checks whether the message is bounced and extracts ' failed address ' from bounced message. Valid only for Communigate Pro servers Function GetInvalidEmailAddressCP(ByVal msg As MailMessage) As String Dim str_invalid_email As String = msg.BodyPlainText ' Check if this is a bounced message report If msg.Subject.IndexOf("Undeliverable mail") = -1 Then Return "" End If If msg.From.ToString().IndexOf("MAILER-DAEMON") = -1 Then Return "" End If ' Now we're sure this is a bounced message report Dim i_start As Integer, i_end As Integer i_start = str_invalid_email.IndexOf("to '<") ' Check if bounced message report contains ' "Failed to deliver to " field If i_start = -1 Then Return "" End If ' Get failed address i_start += 5 i_end = str_invalid_email.IndexOf("]", i_start) str_invalid_email.Substring(i_start, i_end) Return str_invalid_email End Function ' This function must remove (or disable) specified ' email address from mailing list Sub RemoveEmailFromDatabase(ByVal str_invalid_email As String) ' TODO: Add your code here End Sub
回复 (0)
微信扫码 立即评论