OurJS


OurJS-我们的JS, 我们的技术-IT文摘; 专注JS相关领域;
我们热爱编程, 我们热爱技术;我们是高大上, 有品味的码农;

欢迎您订阅我们的技术周刊


我们会向您分享我们精心收集整理的,最新的行业资讯,技术动态,外文翻译,热点文章;
我们使用第三方邮件列表向您推送,我们不保存您的任何个人资料,注重您的隐私,您可以随时退订,

欢迎分享您的观点,经验,技巧,心得

让我们一起找寻程序员的快乐,探索技术, 发现IT人生的乐趣;


本网站使用缓存技术每次加载仅需很小流量, 可在手机中流畅浏览;
如果您发现任何BUG,请即时告知我们: ourjs(at)ourjs.com

MailBee.NET Objects退回邮件教程(二):POP3部分


分享到
关键字 分享   发布 Harriet666  1503567760428
注意 转载须保留原文链接,译文链接,作者译者等信息。  
MailBee.NET Objects是一款为创建、发送、接收以及处理电子邮件而设计的健壮、功能丰富的.NET控件。几行代码便可为应用程序添加E-Mail支持,
简单高效。具备“必需”以及独特的功能,这些控件帮助开发人员简单快速地将复杂的电子邮件功能添加到他们的应用程序中。

MailBee.NET Objects介绍和试用点击查看>>>
 
 
本文主要介绍了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
原文地址: 点此
社区评论 ( Beta版 )
OnceDoc 您自己的企业内容管理系统——文档、流程、知识库、报表、网盘All In One

访问404页面,寻找丢失儿童
 关键字 - 分享
  1. 高报酬寻安卓APP马甲和iOS马甲上架技术人员, 上架一个最高30000元
  2. 六款互联网人不容错过的软件神器
  3. 【示例教程】如何通过leadtools读取二维码中的中文
  4. 【更新】Windows网络守门人UserLock更新至v9.6,可通过Webhooks获取实时登录通知
  5. 2015年度开发者调查:JS最流行;ObjC工资最高;最想学Android
  6. MyEclipse文本编辑器
  7. MyEclipse WebSphere开发教程:WebSphere 7安装指南(四)
  8. 【重大更新】DevExpress v17.2震撼发布|附下载
  9. 招专业APP,封装上架 ,马甲包上架
  10. 干货满满!20篇IntelliJ IDEA使用技巧整理!

 欢迎订阅 - 技术周刊

我们热爱编程, 我们热爱技术; 我们是高端, 大气, 上档次, 有品味, 时刻需要和国际接轨的码农; 欢迎您订阅我们的技术周刊; 您只需要在右上角输入您的邮箱即可; 我们注重您的隐私,您可以随时退订.
加入我们吧! 让我们一起找寻码农的快乐,探索技术, 发现IT人生的乐趣;


 关注我们

我们的微信公众号: ourjs-com
打开微信扫一扫即可关注我们:
IT文摘-程序员(码农)技术周刊

ourjs官方微信号