在C#中,是否有内置的方法可以访问IMAP服务器(使用SSL),或者是否有好用的免费库可用?
在C#中,是否有内置的方法可以访问IMAP服务器(使用SSL),或者是否有好用的免费库可用?
我已经寻找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页面获取最新版本和更好的代码示例。
在.NET框架中没有IMAP的支持,需要使用第三方组件。
尝试使用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);
}
请注意,这是我创建的商业产品。MailSystem.NET 包含了您在 IMAP4 中所需的一切。它是免费且开源的。
(我参与了这个项目)
http://www.codeproject.com/KB/IP/imaplibrary.aspx
另外,还有xemail,它具有SSL参数:http://xemail-net.sourceforge.net/
【编辑】如果您(或客户)有足够的资金购买专业的邮件客户端,这个帖子提供了一些不错的推荐: .NET组件访问电子邮件收件箱的推荐