是否有在 C# 中访问 Imap 服务器(使用 SSL)的内置方法,或者是否有一个好的免费库?
6 回答
我一直在寻找 IMAP 解决方案一段时间,在尝试了很多之后,我将使用AE.Net.Mail。
您可以通过转到“代码”选项卡并单击小的“下载”图标来下载代码。由于作者不提供任何预编译下载,您必须自己编译。(我相信你可以通过 NuGet 获得它)。bin/ 文件夹中不再有 .dll。
没有文档,我认为这是一个缺点,但我能够通过查看源代码(开源!)并使用 Intellisense 来解决这个问题。下面的代码专门连接到 Gmail 的 IMAP 服务器:
// Connect to the IMAP server. The 'true' parameter specifies to use SSL
// which is important (for Gmail at least)
ImapClient ic = new ImapClient("imap.gmail.com", "name@gmail.com", "pass",
ImapClient.AuthMethods.Login, 993, true);
// Select a mailbox. Case-insensitive
ic.SelectMailbox("INBOX");
Console.WriteLine(ic.GetMessageCount());
// Get the first *11* messages. 0 is the first message;
// and it also includes the 10th message, which is really the eleventh ;)
// MailMessage represents, well, a message in your mailbox
MailMessage[] mm = ic.GetMessages(0, 10);
foreach (MailMessage m in mm)
{
Console.WriteLine(m.Subject);
}
// Probably wiser to use a using statement
ic.Dispose();
请务必查看Github页面以获取最新版本和一些更好的代码示例。
IMAP 没有 .NET 框架支持。您需要使用一些第 3 方组件。
试试https://www.limilabs.com/mail,它非常实惠且易于使用,它还支持 SSL:
using(Imap imap = new Imap())
{
imap.ConnectSSL("imap.company.com");
imap.Login("user", "password");
imap.SelectInbox();
List<long> uids = imap.SearchFlag(Flag.Unseen);
foreach (long uid in uids)
{
string eml = imap.GetMessageByUID(uid);
IMail message = new MailBuilder()
.CreateFromEml(eml);
Console.WriteLine(message.Subject);
Console.WriteLine(message.TextDataString);
}
imap.Close(true);
}
请注意,这是我创建的商业产品。
您可以在此处下载:https ://www.limilabs.com/mail 。
MailSystem.NET包含您对 IMAP4 的所有需求。它是免费和开源的。
(我参与了这个项目)
尝试使用该库: https ://imapx.codeplex.com/
该库免费、开源并在此有示例: https ://imapx.codeplex.com/wikipage?title=Sample%20code%20for%20get%20messages%20from%20your%20inbox
我自己没有尝试过,但这是一个你可以尝试的免费库(我不太确定这个库的 SSL 部分):
http://www.codeproject.com/KB/IP/imaplibrary.aspx
此外,还有 xemail,它具有 SSL 参数:
http://xemail-net.sourceforge.net/
[编辑] 如果您(或客户)有钱购买专业的邮件客户端,这个线程有一些很好的建议: