首页 / 知识

关于Unicode:在C#中使用Pop3阅读电子邮件

2023-04-14 05:48:00

关于Unicode:在C#中使用Pop3阅读电子邮件

Reading Email using Pop3 in C#

我正在寻找一种在C#2.0中使用Pop3阅读电子邮件的方法。 当前,我正在使用CodeProject中的代码。 但是,此解决方案并不理想。 最大的问题是它不支持用unicode编写的电子邮件。


我已经成功使用OpenPop.NET通过POP3访问电子邮件。


通过POP3协议下载电子邮件是任务的轻松部分。该协议非常简单,如果您不想通过网络发送明文密码(并且不能使用SSL加密的通信通道),那么唯一困难的部分就是高级身份验证方法。请参阅RFC 1939:邮局协议-版本3
和RFC 1734:POP3 AUTHentication命令以获取详细信息。

当您必须解析收到的电子邮件时,困难的部分就来了,这意味着在大多数情况下,解析为MIME格式。您可以在几小时或几天内编写快速且肮脏的MIME解析器,它将处理所有传入消息的95 +%。改进解析器,使其可以解析几乎所有电子邮件意味着:

  • 获取最流行的邮件客户端发送的电子邮件样本,并改进解析器,以修复由它们产生的错误和RFC错误解释。
  • 确保违反RFC的消息标头和内容的消息不会使解析器崩溃,并且您将能够从错误的电子邮件中读取每个可读或可猜测的值
  • 正确处理国际化问题(例如,从右到左书写的语言,特定语言的正确编码等)
  • 统一码
  • 附件和分层消息项树,如" Mime酷刑电子邮件样本"中所示
  • S / MIME(已签名和加密的电子邮件)。
  • 等等

调试功能强大的MIME解析器需要花费数月的时间。我知道,因为我正在看着我的朋友为下面提到的组件编写一个这样的解析器,并且也在为它编写一些单元测试;-)

回到原来的问题。

以下从POP3教程页面和链接中获取的代码将为您提供帮助:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
// create client, connect and log in
Pop3 client = new Pop3();
client.Connect("pop3.example.org");
client.Login("username","password");

// get message list
Pop3MessageCollection list = client.GetMessageList();

if (list.Count == 0)
{
    Console.WriteLine("There are no messages in the mailbox.");
}
else
{
    // download the first message
    MailMessage message = client.GetMailMessage(list[0].SequenceNumber);
    ...
}

client.Disconnect();
  • HOWTO:从C#中的GMail帐户下载电子邮件(博客文章)
  • Rebex Mail for .NET(.NET的POP3 / IMAP客户端组件)
  • Rebex Secure Mail for .NET(用于.NET的POP3 / IMAP客户端组件-已启用SSL)


我的开源应用程序BugTracker.NET包含一个可以解析MIME的POP3客户端。 POP3代码和MIME代码均来自其他作者,但是您可以看到它们如何在我的应用程序中组合在一起。

对于MIME解析,我使用http://anmar.eu.org/projects/sharpmimetools/。

请参阅文件POP3Main.cs,POP3Client.cs和insert_bug.aspx


您还可以尝试Mail.dll邮件组件,它具有SSL支持,unicode和多国电子邮件支持:

1
2
3
4
5
6
7
8
9
10
11
12
13
using(Pop3 pop3 = new Pop3())
{
    pop3.Connect("mail.host.com");           // Connect to server and login
    pop3.Login("user","password");

    foreach(string uid in pop3.GetAll())
    {
        IMail email = new MailBuilder()
            .CreateFromEml(pop3.GetMessageByUID(uid));
          Console.WriteLine( email.Subject );
    }
    pop3.Close(false);      
}

您可以在这里https://www.limilabs.com/mail下载

请注意,这是我创建的商业产品。


HigLabo.Mail易于使用。这是一个示例用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using (Pop3Client cl = new Pop3Client())
{
    cl.UserName ="MyUserName";
    cl.Password ="MyPassword";
    cl.ServerName ="MyServer";
    cl.AuthenticateMode = Pop3AuthenticateMode.Pop;
    cl.Ssl = false;
    cl.Authenticate();
    ///Get first mail of my mailbox
    Pop3Message mg = cl.GetMessage(1);
    String MyText = mg.BodyText;
    ///If the message have one attachment
    Pop3Content ct = mg.Contents[0];        
    ///you can save it to local disk
    ct.DecodeData("your file path");
}

您可以从https://github.com/higty/higlabo或Nuget [HigLabo]获取它


我不推荐OpenPOP。我只是花了几个小时来调试一个问题-OpenPOP的POPClient.GetMessage()神秘地返回了null。我调试了它,发现它是一个字符串索引错误-请参阅我在此处提交的补丁:http://sourceforge.net/tracker/?func=detail&aid=2833334&group_id=92166&atid=599778。很难找到原因,因为有空的catch {}块吞下了异常。

另外,该项目大部分处于休眠状态,最新版本是2004年。

目前,我们仍在使用OpenPOP,但我将看看人们在这里推荐的其他一些项目。


称我为老式,但为什么要使用第三方库作为简单协议。我已经使用System.Net.Sockets.TCPClient和System.Net.Security.SslStream在基于Web的ASP.NET应用程序中实现了POP3阅读器,以进行加密和身份验证。就协议而言,一旦您打开与POP3服务器的通信,就只需要处理少数命令。这是一个非常容易使用的协议。


我刚刚尝试了SMTPop,它起作用了。

  • 我下载了这个。
  • 为我的C#.NET项目添加了smtpop.dll引用
  • 编写以下代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;    
    using SmtPop;

    namespace SMT_POP3 {

        class Program {
            static void Main(string[] args) {
                SmtPop.POP3Client pop = new SmtPop.POP3Client();
                pop.Open("<hostURL>", 110,"<username>","<password>");

                // Get message list from POP server
                SmtPop.POPMessageId[] messages = pop.GetMailList();
                if (messages != null) {

                    // Walk attachment list
                    foreach(SmtPop.POPMessageId id in messages) {
                        SmtPop.POPReader reader= pop.GetMailReader(id);
                        SmtPop.MimeMessage msg = new SmtPop.MimeMessage();

                        // Read message
                        msg.Read(reader);
                        if (msg.AddressFrom != null) {
                            String from= msg.AddressFrom[0].Name;
                            Console.WriteLine("from:" + from);
                        }
                        if (msg.Subject != null) {
                            String subject = msg.Subject;
                            Console.WriteLine("subject:"+ subject);
                        }
                        if (msg.Body != null) {
                            String body = msg.Body;
                            Console.WriteLine("body:" + body);
                        }
                        if (msg.Attachments != null && false) {
                            // Do something with first attachment
                            SmtPop.MimeAttachment attach = msg.Attachments[0];

                            if (attach.Filename =="data") {
                               // Read data from attachment
                               Byte[] b = Convert.FromBase64String(attach.Body);
                               System.IO.MemoryStream mem = new System.IO.MemoryStream(b, false);

                               //BinaryFormatter f = new BinaryFormatter();
                               // DataClass data= (DataClass)f.Deserialize(mem);
                               mem.Close();
                            }                    

                            // Delete message
                            // pop.Dele(id.Id);
                        }
                   }
               }    
               pop.Quit();
            }
        }
    }

    电子邮件解决方案方法支持

    最新内容

    相关内容

    热门文章

    推荐文章

    标签云

    猜你喜欢