From: POSTMASTER@domain.comTo: jdoe@domain.comSubject: Message Delivery FailureMailEnable: 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.localTo: jdoe@localhost Subject:Undeliverable mail:Failed to deliver to '<bill@localhost>'
正如你所看到的,这个退回的邮件与前一个几乎相同。使用哪个服务器并不重要,退回信息总是有From,To和Subject字段,退回信息在其正文中有失败的地址。函数GetInvalidEmailAddressCP()与前一个函数几乎相同; 区别仅在于检查邮件字段并搜索失败的地址。支持尽可能多的退回电子邮件格式至关重要。这就是为什么你需要修改GetInvalidEmailAddress()函数,以适应服务器使用的退回电子邮件格式。 注意:在某些情况下,你可能需要扫描退回的电子邮件以获取其他信息。如果由于暂时的问题,退回的邮件到达,则不要删除电子邮件地址:邮箱超过配额,邮件服务器关闭等。退回邮件可以分为两种类型:硬邮件和软邮件。如果是不存在的帐户或域,则会发生硬性退回。其他故障,如完整的邮箱或暂时不可用的域,则是软性退回。你可以为数据库中的每个地址进行计算。也许下一次尝试发送邮件到具有一些软性退回的地址时将成功。 代码示例:在此示例中,我们从指定的帐户中检索退回的电子邮件,然后从每个退回的电子邮件中提取失败的电子邮件地址。失败的地址传递给RemoveEmailFromDatabase子程序,该子程序必须从数据库中删除失败的地址。应用程序还会从服务器中删除退回的电子邮件,以避免下次处理。C#:
// Create POP3 objectPop3 pop = new Pop3();// Enable logging to filepop.Log.Enabled = true;pop.Log.Filename = @"C:log.txt";pop.Log.Clear();// Connect to POP3 serverpop.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 mailboxforeach (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 serverpop.Disconnect();// The function checks whether the message is bounced and extracts// failed address// from bounced message. Valid only for MailEnable serversstatic 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 serversstatic 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 liststatic void RemoveEmailFromDatabase(string str_invalid_email){// TODO: Add your code here }
VB.NET:
Dim pop As New Pop3' Enable logging to filepop.Log.Enabled = Truepop.Log.Filename = "C:log.txt"pop.Log.Clear()' Connect to POP3 serverpop.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 mailboxDim msg As MailMessageFor 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 IfNextConsole.ReadLine()' Disconnect from POP3 serverpop.Disconnect()' The function checks whether the message is bounced and extracts' failed address' from bounced message. Valid only for MailEnable serversFunction 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_emailEnd Function' The function checks whether the message is bounced and extracts' failed address' from bounced message. Valid only for Communigate Pro serversFunction 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_emailEnd Function' This function must remove (or disable) specified' email address from mailing listSub RemoveEmailFromDatabase(ByVal str_invalid_email As String) ' TODO: Add your code here End Sub
标签:电子邮件
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!