博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# 利用MailKit.IMap 收取163邮件
阅读量:7091 次
发布时间:2019-06-28

本文共 3694 字,大约阅读时间需要 12 分钟。

最近我要做一个爬虫。这个爬虫需要如下几个步骤:

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
uids = client.Search(SearchCondition.Unseen()); // Download mail messages from the default mailbox. IEnumerable
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); } }

转载于:https://www.cnblogs.com/waw/p/8494594.html

你可能感兴趣的文章
MySQL索引原理及慢查询优化
查看>>
JNI开发示例
查看>>
从netty-example分析Netty组件
查看>>
[经验]无线鼠标和无线键盘真的不能用了?——雷柏的重生之路~
查看>>
HTTP协议
查看>>
django的hello world 项目
查看>>
canvas线条的属性
查看>>
基础006_pg109_IP-Xfft
查看>>
[二] java8 函数式接口详解 函数接口详解 lambda表达式 匿名函数 方法引用使用含义 函数式接口实例 如何定义函数式接口...
查看>>
为什么 c = tf.matmul(a, b) 不立即执行矩阵乘法?
查看>>
Linux 变量引用和命令替换
查看>>
各大互联网公司架构演进之路汇总
查看>>
[转载].NET平台测试驱动开发模拟框架Moq简明教程(简介)
查看>>
xmlHttp
查看>>
Eclipse编译报Javascript和jquery错的解决办法
查看>>
C#字符串的简单用法
查看>>
EXPORT_SYMBOL的使用并以使用do_adjtimex调节内核tick_length(滴答长度)为例的说明...
查看>>
什么是VC维?
查看>>
SuperMap IS.NET自定义Action之兴趣点标注(转)
查看>>
eclipse 创建maven 项目 动态web工程完整示例 maven 整合springmvc整合mybatis
查看>>