最近我要做一個爬蟲。這個爬蟲需要如下幾個步驟:
1 填寫注冊內容(需要郵箱注冊)
2 過拖拽驗證碼(geetest)
3 注冊成功會給郵箱發一封確認郵箱
4 點擊確認郵箱中的鏈接 完成注冊
我這里就采用163郵箱注冊。
郵箱協議有 pop3 和 imap 和 smtp
我試了pop3 不能夠篩選郵件 例如篩選未讀 和 發件人這2個條件 所以放棄用pop3
imap協議是支持的。
我就找了一個開源的第三方lib:S22.Imap
用法很簡單:
public void Test163() { var imapServer = "imap.163.com"; var port = 993; using (ImapClient client = new ImapClient(imapServer, port, "xxxx@163.com", "pwd", AuthMethod.Login, true)) { // Returns a collection of identifiers of all mails matching the specified search criteria. IEnumerable<uint> uids = client.Search(SearchCondition.Unseen()); // Download mail messages from the default mailbox. IEnumerable<MailMessage> messages = client.GetMessages(uids,FetchOptions.HtmlOnly); Console.WriteLine("We are connected!"); } }
發現 在login的時候 報錯了:
提示“NO Select Unsafe Login. Please contact kefu@188.com for help”。
163郵箱也會收到一個告警郵件
經過查證 發現得需要在發送 login 命令之前 得先發送 id 命令
至於為什么要這么做 我的理解是得先偽裝成普通的客戶端吧(有理解錯誤請指出謝謝)
我fork了一份SS2.imap的代碼 打算兼容163的這個特殊情況改掉源碼
然后走Login方法就不會報錯了
Github地址:https://github.com/yuzd/S22.Imap
http://www.mimekit.net/docs/html/M_MailKit_Net_Imap_ImapClient_Identify.htm
public static void Capabilities () { using (var client = new ImapClient ()) { client.Connect ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect); var mechanisms = string.Join (", ", client.AuthenticationMechanisms); Console.WriteLine ("The IMAP server supports the following SASL authentication mechanisms: {0}", mechanisms); client.Authenticate ("username", "password"); if (client.Capabilities.HasFlag (ImapCapabilities.Id)) { var clientImplementation = new ImapImplementation { Name = "MailKit", Version = "1.0" }; var serverImplementation = client.Identify (clientImplementation); Console.WriteLine ("Server implementation details:"); foreach (var property in serverImplementation.Properties) Console.WriteLine (" {0} = {1}", property.Key, property.Value); } if (client.Capabilities.HasFlag (ImapCapabilities.Acl)) { Console.WriteLine ("The IMAP server supports Access Control Lists."); Console.WriteLine ("The IMAP server supports the following access rights: {0}", client.Rights); Console.WriteLine ("The Inbox has the following access controls:"); var acl = client.Inbox.GetAccessControlList (); foreach (var ac in acl) Console.WriteLine (" {0} = {1}", ac.Name, ac.Rights); var myRights = client.Inbox.GetMyAccessRights (); Console.WriteLine ("Your current rights for the Inbox folder are: {0}", myRights); } if (client.Capabilities.HasFlag (ImapCapabilities.Quota)) { Console.WriteLine ("The IMAP server supports quotas."); Console.WriteLine ("The current quota for the Inbox is:"); var quota = client.Inbox.GetQuota (); if (quota.StorageLimit.HasValue && quota.StorageLimit.Value) Console.WriteLine (" Limited by storage space. Using {0} out of {1} bytes.", quota.CurrentStorageSize.Value, quota.StorageLimit.Value); if (quota.MessageLimit.HasValue && quota.MessageLimit.Value) Console.WriteLine (" Limited by the number of messages. Using {0} out of {1} bytes.", quota.CurrentMessageCount.Value, quota.MessageLimit.Value); Console.WriteLine ("The quota root is: {0}", quota.QuotaRoot); } if (client.Capabilities.HasFlag (ImapCapabilities.Thread)) { if (client.ThreadingAlgorithms.Contains (ThreadingAlgorithm.OrderedSubject)) Console.WriteLine ("The IMAP server supports threading by subject."); if (client.ThreadingAlgorithms.Contains (ThreadingAlgorithm.References)) Console.WriteLine ("The IMAP server supports threading by references."); } client.Disconnect (true); } }