【发布时间】:2010-09-07 20:19:21
【问题描述】:
我正在寻找一种在 C# 2.0 中使用 Pop3 阅读电子邮件的方法。目前,我正在使用CodeProject 中的代码。然而,这种解决方案并不理想。最大的问题是它不支持用 unicode 编写的电子邮件。
【问题讨论】:
我正在寻找一种在 C# 2.0 中使用 Pop3 阅读电子邮件的方法。目前,我正在使用CodeProject 中的代码。然而,这种解决方案并不理想。最大的问题是它不支持用 unicode 编写的电子邮件。
【问题讨论】:
我已成功使用OpenPop.NET 通过 POP3 访问电子邮件。
【讨论】:
通过 POP3 协议下载电子邮件是任务的简单部分。该协议非常简单,如果您不想通过网络发送明文密码(并且不能使用 SSL 加密通信通道),唯一困难的部分可能是高级身份验证方法。详情请参阅RFC 1939: Post Office Protocol - Version 3 和RFC 1734: POP3 AUTHentication command。
当您必须解析收到的电子邮件时,困难的部分来了,这意味着在大多数情况下解析 MIME 格式。您可以在几个小时或几天内编写快速而肮脏的 MIME 解析器,它将处理 95+% 的所有传入消息。改进解析器,使其几乎可以解析任何电子邮件意味着:
调试强大的 MIME 解析器需要几个月的时间。我知道,因为我正在看着我的朋友为下面提到的组件编写一个这样的解析器,并且也在为它编写一些单元测试;-)
回到原来的问题。
关注code taken from our POP3 Tutorial page 和链接将对您有所帮助:
//
// 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 包含一个可以解析 MIME 的 POP3 客户端。 POP3 代码和 MIME 代码都来自其他作者,但您可以在我的应用中看到它们是如何组合在一起的。
对于 MIME 解析,我使用 http://anmar.eu.org/projects/sharpmimetools/。
查看文件 POP3Main.cs、POP3Client.cs 和 insert_bug.aspx
【讨论】:
你也可以试试Mail.dll mail component,它支持 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
请注意,这是我创建的商业产品。
【讨论】:
称我为老式但为什么要使用 3rd 方库来实现简单的协议。我已经在基于 Web 的 ASP.NET 应用程序中使用 System.Net.Sockets.TCPClient 和 System.Net.Security.SslStream 实现了 POP3 阅读器,用于加密和身份验证。就协议而言,一旦您打开与 POP3 服务器的通信,您只需处理少量命令。这是一个非常容易使用的协议。
【讨论】:
我不会推荐 OpenPOP。我只花了几个小时调试一个问题——OpenPOP 的 POPClient.GetMessage() 神秘地返回 null。我对此进行了调试,发现这是一个字符串索引错误 - 请参阅我在此处提交的补丁:http://sourceforge.net/tracker/?func=detail&aid=2833334&group_id=92166&atid=599778。由于存在吞噬异常的空 catch{} 块,因此很难找到原因。
此外,该项目大多处于休眠状态...最后一次发布是在 2004 年。
目前我们仍在使用 OpenPOP,但我会看看人们在这里推荐的其他一些项目。
【讨论】:
HigLabo.Mail 易于使用。这是一个示例用法:
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] 获得它
【讨论】:
我刚刚尝试了 SMTPop,它成功了。
smtpop.dll 对我的 C# .NET 项目的引用写了如下代码:
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();
}
}
}
【讨论】: