我在寻找一个方法阅读电子邮件使用Pop3在C#2.0.目前,我正在使用代码中找到的 演示.然而,这种解决方案是不太理想。最大的问题是,它不支持的电子邮件写在unicode。

有帮助吗?

解决方案

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

其他提示

下载的电子邮件,通过POP3协议是容易的部分的任务。该协议是很简单,唯一的困难的部分可能高级认证方法如果你不想要发出一个明确的密码文本通过网络(及不能使用SSL加密的通信频道)。看看 RFC1939年:Post Office Protocol-3版 RFC1734:POP3认证的命令 对于细节。

最难的部分来的时候你必须分析收到的电子邮件,这意味着分析MIME格式在大多数情况下。你可以写快速和肮脏的MIME分析器在几小时或几天内,它将处理95+%的所有收到的消息。提高解析,因此它可以分析几乎所有的电子邮件是指:

  • 获得电子邮件样品发送的最受欢迎的电子邮件用户和提高分析程序,以便修复的错误和RFC误解产生由他们。
  • 确保消违反RFC为消息的标题和内容不会崩溃你的分析器,你将能够阅读的每一个可读或猜测的价值从出错的电子邮件
  • 正确处理国际化问题(例如语言写的。左,正确的编码为特定的语言等)
  • UNICODE
  • 附件以及分层消息项树可以看到 "Mime酷刑的电子邮件样本"
  • S/MIME(签名和加密电子邮件).
  • 等等

调试一个强大的MIME析需要几个月的工作。我知道,因为我看着我的朋友写一个这样的分析器组件下面提到的并且是书面的几个单元的测试,太;-)

回到原来的问题。

代码,从我们的POP3页的教程 链接会帮助你:

// 
// 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();

我的开放源应用程序 BugTracker.NET 包括POP3客户,可以分析MIME。两POP3码和MIME码从其他作者,但是你可以看到这一切是如何结合在一起在我的应用程序。

为MIME分析,我用 http://anmar.eu.org/projects/sharpmimetools/.

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

你也可以试试 Mail.dll 邮件组件, 它已SSL支持,unicode,多国家的电子邮件支持:

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

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

我不会推荐OpenPOP.我只是花了几个小时的调试一个问题-OpenPOP的POPClient.GetMessage()是神秘地返回空。我调试这并发现这是一个串索引的错误看到的补丁我提交了这里: http://sourceforge.net/tracker/?func=detail&aid=2833334&group_id=92166&atid=599778.这是难以找到原因,因为那里是空的捕捉{}块吞例外情况。

此外,该项目的主要是处于休眠...最后一个版本是在2004年。

现在我们仍在使用OpenPOP,但我会看一看一些其他项目人们已建议在这里。

HigLabo.邮件是易于使用。这里是一样的使用情况:

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]

叫我老的时尚,但是为什么使用一个第3方图书馆为一个简单的协议。我已经实现POP3的读者,在基于网络ASP.NET 应用程序系统。网。插座。TCPClient和系统。网。安全。SslStream于加密和认证。尽的协议中去,一旦你打开了通POP3server,只有极少数的命令,你要处理的。这是一个非常简单的议定书》的工作。

我只是试图SMTPop和它的工作。

  1. 我下载了 .
  2. 加入 smtpop.dll 参照我的。项目净

写下代码:

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();
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top