有没有人有任何示例代码使用通过IMAP SSL连接到googlemail服务器的.Net框架来检查新电子邮件?
您可能会对此处列出的URL感兴趣
http://www.codeplex.com/InterIMAP
这是扩展到
http://www.codeproject.com/KB/IP/imaplibrary.aspx?fid=91819&df=90&mpp=25&noise=5&sort=Position&view=Quick&fr=26&select=2562067#xx2562067xx
我建议看MailKit,因为它可能是最强大的邮件库,它是开源(MIT).
关于MailKit的一个很棒的事情是所有网络API都是可取消的(我在任何其他IMAP库中都没有看到过).
它也是我所知道的唯一支持消息线程的库.
using System; using System.Net; using System.Threading; using MailKit.Net.Imap; using MailKit.Search; using MailKit; using MimeKit; namespace TestClient { class Program { public static void Main (string[] args) { using (var client = new ImapClient ()) { using (var cancel = new CancellationTokenSource ()) { client.Connect ("imap.gmail.com", 993, true, cancel.Token); // If you want to disable an authentication mechanism, // you can do so by removing the mechanism like this: client.AuthenticationMechanisms.Remove ("XOAUTH"); client.Authenticate ("joey", "password", cancel.Token); // The Inbox folder is always available... var inbox = client.Inbox; inbox.Open (FolderAccess.ReadOnly, cancel.Token); Console.WriteLine ("Total messages: {0}", inbox.Count); Console.WriteLine ("Recent messages: {0}", inbox.Recent); // download each message based on the message index for (int i = 0; i < inbox.Count; i++) { var message = inbox.GetMessage (i, cancel.Token); Console.WriteLine ("Subject: {0}", message.Subject); } // let's try searching for some messages... var query = SearchQuery.DeliveredAfter (DateTime.Parse ("2013-01-12")) .And (SearchQuery.SubjectContains ("MailKit")) .And (SearchQuery.Seen); foreach (var uid in inbox.Search (query, cancel.Token)) { var message = inbox.GetMessage (uid, cancel.Token); Console.WriteLine ("[match] {0}: {1}", uid, message.Subject); } client.Disconnect (true, cancel.Token); } } } } }
作为上述项目的作者,我可以说它确实支持SSL.
我目前正在开发一个新版本的库,它将完全异步,以提高它与IMAP服务器交互的速度.
该代码虽然不完整,但可以从链接到上面的代码plex站点下载原始同步库(也支持SSL).
Cross从其他类似问题发布.看看当它们如此相似时会发生什么?
我一直在寻找IMAP解决方案一段时间,经过不少尝试后,我将选择AE.Net.Mail.
没有文档,我认为这是一个缺点,但我能够通过查看源代码(yay for open source!)和使用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();
我不隶属于这个库或任何东西,但我发现它非常快速和稳定.
Lumisoft.net有你可以使用的IMAP客户端和服务器代码.
我用它来从Gmail下载电子邮件.对象模型不是最好的,但它是可行的,并且似乎相当灵活和稳定.
这是我使用它的尖峰的部分结果.它使用信封获取前10个标题,然后获取完整的消息:
using (var client = new IMAP_Client()) { client.Connect(_hostname, _port, _useSsl); client.Authenticate(_username, _password); client.SelectFolder("INBOX"); var sequence = new IMAP_SequenceSet(); sequence.Parse("0:10"); var fetchItems = client.FetchMessages(sequence, IMAP_FetchItem_Flags.Envelope | IMAP_FetchItlags.UID, false, true); foreach (var fetchItem in fetchItems) { Console.Out.WriteLine("message.UID = {0}", fetchItem.UID); Console.Out.WriteLine("message.Envelope.From = {0}", fetchItem.Envelope.From); Console.Out.WriteLine("message.Envelope.To = {0}", fetchItem.Envelope.To); Console.Out.WriteLine("message.Envelope.Subject = {0}", fetchItem.Envelope.Subject); Console.Out.WriteLine("message.Envelope.MessageID = {0}", fetchItem.Envelope.MessageID); } Console.Out.WriteLine("Fetching bodies"); foreach (var fetchItem in client.FetchMessages(sequence, IMAP_FetchItem_Flags.All, false, true) { var email = LumiSoft.Net.Mail.Mail_Message.ParseFromByte(fetchItem.MessageData); Console.Out.WriteLine("email.BodyText = {0}", email.BodyText); } }
IMAP没有.NET框架支持.您需要使用一些第三方组件.
试试Mail.dll电子邮件组件,它非常实惠且易于使用,它还支持SSL:
using(Imap imap = new Imap()) { imap.ConnectSSL("imap.company.com"); imap.Login("user", "password"); imap.SelectInbox(); Listuids = imap.Search(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.Text); } imap.Close(true); }
请注意,这是我创建的商业产品.
您可以在此处下载:https://www.limilabs.com/mail.
MailSystem.NET包含您对IMAP4的所有需求.它是免费和开源的.
(我参与了这个项目)