天天看點

c# 利用IMap 收取163郵件

最近我要做一個爬蟲。這個爬蟲需要如下幾個步驟:

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, "[email protected]", "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 [email protected] for help”。

163郵箱也會收到一個告警郵件

c# 利用IMap 收取163郵件

經過查證 發現得需要在發送 login 指令之前 得先發送 id 指令

至于為什麼要這麼做 我的了解是得先僞裝成普通的用戶端吧(有了解錯誤請指出謝謝)

我fork了一份SS2.imap的代碼 打算相容163的這個特殊情況改掉源碼

c# 利用IMap 收取163郵件
c# 利用IMap 收取163郵件

然後走Login方法就不會報錯了

c# 利用IMap 收取163郵件

Github位址:https://github.com/yuzd/S22.Imap

如果您覺得閱讀本文對您有幫助,請點一下“推薦”按鈕,您的“推薦”将是我最大的寫作動力!歡迎各位轉載,轉載文章之後須在文章頁面明顯位置給出作者和原文連接配接,謝謝。