我正在寻找一种在C#2.0中使用Pop3阅读电子邮件的方法。目前,我正在使用CodeProject中的代码。但是,这种解决方案并不理想。最大的问题是它不支持用unicode编写的电子邮件。在C中使用Pop3阅读电子邮件#
回答
我已成功使用OpenPop.NET通过POP3访问电子邮件。
通过POP3协议下载电子邮件是该任务的简单部分。该协议非常简单,如果您不想通过网络发送明文密码(并且无法使用SSL加密通信通道),则唯一难以完成的部分可能是高级身份验证方法。有关详细信息,请参阅RFC 1939: Post Office Protocol - Version 3 和RFC 1734: POP3 AUTHentication command。
当您必须解析收到的电子邮件时,这意味着大多数情况下解析MIME格式。您可以在几个小时或几天内编写快速的&脏MIME解析器,它将处理所有传入邮件的95%以上。提高分析器因此它可以分析几乎任何电子邮件方式:为了解决它们产生的错误和误解的RFC从最流行的邮件客户端发送
- 越来越电子邮件样本,改善解析器。
- 确保违反RFC的邮件标题和内容的消息会不会崩溃您的解析器和你将能够从错位的电子邮件
- (国际化问题的正确处理如语言阅读每一个可读或可猜测值从右击写到左,正确的编码为特定的语言等)
- UNICODE
- 附件和层次的消息项目树看到"Mime torture email sample"
- S/MIME(签名和加密的电子邮件)。
- 等
调试一个强大的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();
基本上你说的是“买我的组件”,对吧?没有错,这听起来像是一个很好的组件。 – MarkJ 2009-11-06 12:31:01
您可以尝试任何第三方组件(免费或商业)。我的这篇文章试图指出,编写这样的组件既困难又费时,因为需要进行大量测试 - 如果没有大量真实用户的数据进行大量错误报告,您几乎无法做到这一点。如果您选择Rebex组件,那将会很不错,但是如果您选择另一组件,我对它没有任何问题。编写自己的MIME解析器或使用Web上发现的一些概念验证代码是恕我直言,在这种情况下,不是最好的方法。但是我可能会受到偏见;-),先画出自己的结论并测试代码。 – 2009-11-06 19:21:16
我可以使用Rebex组件从Exchange 2003收件箱中获取邮件吗? – Kiquenet 2010-11-23 09:46:53
叫我老的方式,但为什么要使用第三方库为一个简单的协议。我已经在基于Web的ASP.NET应用程序中使用System.Net.Sockets.TCPClient和System.Net.Security.SslStream实现了POP3读取器,以进行加密和身份验证。就协议而言,一旦开启了与POP3服务器的通信,就只有少数命令需要处理。与其合作非常简单。
我的开源应用程序BugTracker.NET包含一个可以解析MIME的POP3客户端。 POP3代码和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
请注意,这是我创建的商业产品。
我不会推荐OpenPOP。我花了几个小时来调试一个问题 - OpenPOP的POPClient.GetMessage()神秘地返回null。我调试了这个,发现它是一个字符串索引bug - 请参阅我在此提交的补丁: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和它的工作得到它。
- 我下载了this。
- 增加
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();
}
}
}
- 1. 阅读目标C中的电子邮件(例如pop3)
- 2. 阅读pop3电子邮件,为什么不读取电子邮件?
- 3. 使用iPhone SDK通过POP3阅读电子邮件
- 4. 只读未读电子邮件使用pop3 c#
- 5. 使用OpenPop.Net阅读pop3邮件
- 6. 如何阅读MIME格式的电子邮件pop3
- 7. 阅读电子邮件在asp.net网页上使用Pop3客户端
- 8. 电子邮件阅读/未读邮件
- 9. 阅读在c#中使用Openpop的流行电子邮件
- 10. Pop3用于标记的命令被读为电子邮件为阅读电子邮件
- 11. 如何使用pop3读取雅虎邮件的最新电子邮件c#
- 12. 使用Silverlight阅读电子邮件
- 13. POP3接收电子邮件编码C#
- 14. Google.GData.Client阅读电子邮件
- 15. 阅读电子邮件?
- 16. silverlight阅读电子邮件
- 17. 阅读电子邮件
- 18. Python - 阅读电子邮件?
- 19. 如何使用android自动阅读Pop3中的未读邮件
- 20. 使用SQL Server 2008读取POP3电子邮件帐户
- 21. 用groovy阅读电子邮件(Java Mail)
- 22. 阅读电子邮件内容并使用Exchange 2010和c#
- 23. 在R中使用gmail阅读电子邮件正文
- 24. 使用MailKit将电子邮件移至垃圾邮件POP3
- 25. 在Erlang中使用电子邮件(POP3,IMAP,SMTP e.t.c)?
- 26. 如何在codeigniter中使用pop3接收电子邮件
- 27. ZF2阅读电子邮件附件
- 28. 如何阅读电子邮件附件
- 29. 如何阅读来自pop3 in.net和com的多部分/混合电子邮件?
- 30. 智能电子邮件阅读
链接的NuGet:https://www.nuget.org/packages/OpenPop.NET/ – razon 2017-01-13 11:50:30