一个基于POP3协议进行邮箱账号验证的类

最近老陈要针对企业邮箱做一些开发,以对接企业OA神马的,但企业邮箱唯独没有开放账号密码验证功能,很恼火!不得已,翻出早些年的Asp代码改编成了C#类,实现了一个C#下的通过POP3协议进行邮箱账号验证的类,而且还能完美支持SSL加密,貌似很实用的样子,分享给大家先!

无废话,直接放代码:

  1 // ===============================================================================
  2 //  老陈出击,必属精品!
  3 // 
  4 //  Copyright © ymind.net. All rights reserved .
  5 //  官方网站:http://ymind.net/
  6 //  版权所有:彦铭工作室
  7 // ===============================================================================
  8 
  9 using System;
 10 using System.IO;
 11 using System.Net.Security;
 12 using System.Net.Sockets;
 13 using System.Text;
 14 
 15 namespace WindowsFormsApplication1
 16 {
 17     /// <summary>
 18     /// 提供通过 POP3 协议进行电子信箱账号验证的功能。
 19     /// </summary>
 20     public sealed class POP3AccountValidator
 21     {
 22         #region ValidateResults enum
 23 
 24         /// <summary>
 25         /// 表示验证结果的枚举值。
 26         /// </summary>
 27         public enum ValidateResults
 28         {
 29             /// <summary>
 30             /// 未指定。
 31             /// </summary>
 32             None = 0,
 33 
 34             /// <summary>
 35             /// 连接失败。
 36             /// </summary>
 37             ConnectFailed = 1,
 38 
 39             /// <summary>
 40             /// 无效的登录账号。
 41             /// </summary>
 42             InvalidUserName = 2,
 43 
 44             /// <summary>
 45             /// 无效的登录密码。
 46             /// </summary>
 47             InvalidPassword = 3,
 48 
 49             /// <summary>
 50             /// 登录成功。
 51             /// </summary>
 52             Success = 4,
 53 
 54             /// <summary>
 55             /// 验证过程发生异常。
 56             /// </summary>
 57             Error = 5,
 58         }
 59 
 60         #endregion
 61 
 62         private const string _CRLF = "\r\n";
 63         private readonly bool _useSSL;
 64 
 65         /// <summary>
 66         /// 初始化 <see cref="POP3AccountValidator"/> 类的新实例。
 67         /// </summary>
 68         /// <param name="server">指定 POP3 服务器。</param>
 69         public POP3AccountValidator(string server) : this(server, 110) { }
 70 
 71         /// <summary>
 72         /// 初始化 <see cref="POP3AccountValidator"/> 类的新实例。
 73         /// </summary>
 74         /// <param name="server">指定 POP3 服务器。</param>
 75         /// <param name="port">指定 POP3 服务器端口号。</param>
 76         public POP3AccountValidator(string server, int port) : this(server, port, false) { }
 77 
 78         /// <summary>
 79         /// 初始化 <see cref="POP3AccountValidator"/> 类的新实例。
 80         /// </summary>
 81         /// <param name="server">指定 POP3 服务器。</param>
 82         /// <param name="port">指定 POP3 服务器端口号。</param>
 83         /// <param name="useSSL">指定一个值,该值指示验证过程是否使用 SSL 加密协议。</param>
 84         public POP3AccountValidator(string server, int port, bool useSSL)
 85         {
 86             if (String.IsNullOrWhiteSpace(server)) throw new ArgumentOutOfRangeException("server");
 87             if (port < 1 || port > 65535) throw new ArgumentOutOfRangeException("port");
 88 
 89             this.Server = server;
 90             this.Port = port;
 91             this._useSSL = useSSL;
 92         }
 93 
 94         /// <summary>
 95         /// 获取 POP3 服务器。
 96         /// </summary>
 97         public string Server { get; private set; }
 98 
 99         /// <summary>
100         /// 获取 POP3 服务器端口号。
101         /// </summary>
102         public int Port { get; private set; }
103 
104         private static ValidateResults _Validate(Stream stream, string username, string password)
105         {
106             var data = "USER " + username + _CRLF;
107 
108             using (var reader = new StreamReader(stream))
109             {
110                 if (!reader.ReadLine().Contains("+OK")) return ValidateResults.ConnectFailed;
111 
112                 var charData = Encoding.ASCII.GetBytes(data);
113 
114                 stream.Write(charData, 0, charData.Length);
115 
116                 if (!reader.ReadLine().Contains("+OK")) return ValidateResults.InvalidUserName;
117 
118                 data = "PASS " + password + _CRLF;
119                 charData = Encoding.ASCII.GetBytes(data);
120 
121                 stream.Write(charData, 0, charData.Length);
122 
123                 return reader.ReadLine().Contains("+OK") ? ValidateResults.Success : ValidateResults.InvalidPassword;
124             }
125         }
126 
127         /// <summary>
128         /// 验证电子信箱账号。
129         /// </summary>
130         /// <param name="username">电子信箱账号。</param>
131         /// <param name="password">电子信箱密码。</param>
132         /// <returns>返回 <see cref="ValidateResults"/> 枚举值之一。</returns>
133         public ValidateResults Validate(string username, string password)
134         {
135             if (username == null) throw new ArgumentNullException("username");
136             if (password == null) throw new ArgumentNullException("password");
137 
138             try
139             {
140                 using (var tcpClient = new TcpClient(this.Server, this.Port))
141                 {
142                     using (var tcpStream = tcpClient.GetStream())
143                     {
144                         if (!this._useSSL) return _Validate(tcpStream, username, password);
145 
146                         using (var sslStream = new SslStream(tcpStream, false))
147                         {
148                             sslStream.AuthenticateAsClient(this.Server);
149 
150                             return _Validate(sslStream, username, password);
151                         }
152                     }
153                 }
154             }
155             catch
156             {
157                 return ValidateResults.Error;
158             }
159         }
160     }
161 }

 

转载于:https://www.cnblogs.com/ymind/p/3384534.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/460741.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

cobbler get-loaders 通过代理下载

2019独角兽企业重金招聘Python工程师标准>>> cobbler 版本是2.6.3&#xff0c;可以通过系统环境变量设置proxy&#xff0c;支持 HTTP_PROXY、HTTPS_PROXY、FTP_PROXY 三个变量。 cobbler 版本是2.6.6时&#xff0c;需要从/etc/cobbler/settings 中增加proxy_url_ex…

Shell文件包含

像其他语言一样&#xff0c;Shell 也可以包含外部脚本&#xff0c;将外部脚本的内容合并到当前脚本。 Shell 中包含脚本可以使用&#xff1a; . filename 或者 source filename 两种方式的效果相同。简单起见&#xff0c;一般使用点号(.)&#xff0c;但是注意点号(.)和文件…

Vim高级使用 - CentOS下使用VIM打造C/C++开发环境

使用TagList http://blog.csdn.net/fbfsber008/article/details/7044723 转载于:https://www.cnblogs.com/tiantao/p/2389126.html

Shell数组:shell数组的定义、数组长度

bash支持一维数组&#xff08;不支持多维数组&#xff09;&#xff0c;并且没有限定数组的大小。类似与C语言&#xff0c;数组元素的下标由0开始编号。获取数组中的元素要利用下标&#xff0c;下标可以是整数或算术表达式&#xff0c;其值应大于或等于0。 定义数组 在Shell中…

分析busybox的源码

以下内容源于网络资源的学习与整理&#xff0c;如有侵权请告知删除。 参考博客 busybox详解_linuxarmsummary的博客-CSDN博客 一、前言 因为uboot给内核传参的bootargs中有“init/linuxrc”这个项目&#xff0c;而由前面的分析可知/linuxrc这个二进制文件位于根文件系统中&…

Vmware vSphere 十个疑难问题解决方法

Vmware vSphere疑难问题解决方法导读&#xff1a;这里汇总了10个Vmware vSphere常见的问题&#xff1a;清除vSphere Client的登录记录、Linux系统VMXNET3虚拟网路卡时UDP包被Drop掉等&#xff0c;并给出了具体的解决办法。关键词&#xff1a;VMware vSphere1、清除vSphere Clie…

PHP中开发的良好习惯总结(持续更新) By ACReaper

最近在自己用php在写一个商城&#xff0c;再这个工程中学习到了很多的开发经验。 1.为了更好的调试和开发php&#xff0c;在开发状态中到php.ini设置开发的错误报告 在ubuntu中&#xff0c;其在/etc/php5/apache2/php.ini中。用命令打开后&#xff0c;找到 error_reporting E_…

Makefile中常用的函数

以下内容源于C语言中文网的学习与整理&#xff0c;非原创&#xff0c;如有侵权请告知删除。 一、Makefile中的函数格式 函数的调用和变量的调用很像。引用变量的格式为$(变量名)&#xff0c;而函数调用的格式如下&#xff1a; $(<function> <arguments>) 或者是…

彻底解决zend studio 下 assignment in condition警告

最近在mac系统下安装zend studio作为php开发工具&#xff0c;把以前的代码导入&#xff0c;发现项目中有很多 “assignment in condition”的警告&#xff0c;造成原因是在条件判断的if、while中使用了如下类似的做法&#xff1a; if ($res $other)while (($row $res->fet…

添加分页

<div style"text-align: center; padding-top: 10px"><webdiyer:AspNetPager ID"AspNetPager1" runat"server" PageSize"20" FirstPageText"首页"CssClass"anpager" LastPageText"尾页" NextP…

Makefile中命令的编写

以下内容源于C语言中文网的学习与整理&#xff0c;非原创&#xff0c;如有侵权请告知删除。 简述 Makefile的规则&#xff0c;是由依赖关系规则和命令组成的。 Makefile中所使用的命令是由 shell 命令行组成&#xff0c;它们是一条一条执行的。 Makefile 中的任何命令都要以ta…

走出浮躁的泥沼:浮躁的社会原因 控制好自己的物欲

http://www.nowamagic.net/librarys/veda/detail/2265现在这个社会&#xff0c;大家都很浮躁。简单说&#xff0c;因为他是消费者。 具体的逻辑推理是这样的&#xff1a; 1. 现在的社会是一个“富裕社会”&#xff08;Affluent Society&#xff09;&#xff0c;物质极大丰富&am…

c# 连接各种数据库 Access、Server等

1.C#连接连接Access程序代码:usingSystem.Data;usingSystem.Data.OleDb;..stringstrConnection"ProviderMicrosoft.Jet.OleDb.4.0;";strConnection"Data SourceC:BegASPNETNorthwind.mdb";OleDbConnection objConnectionnewOleDbConnection(strConnection)…

make命令的参数选项

在执行 make 命令时&#xff0c;有时根据需要&#xff0c;可以添加某些参数选项。比如只打印命令但不执行命令的参数选项是 "-n" &#xff0c;还有只执命令不打印命令的参数选项是 "-s"&#xff0c;包含其它文件的路径参数选项是 "-include"等等…

〖Linux〗Kubuntu设置打开应用时就只在打开时的工作区显示

有没有遇到一种情况&#xff1a; 在工作区1打开了应用程序Google Chrome&#xff1b; 这个时间感觉它打开速度比较慢&#xff0c;就快捷键切换到工作区2了&#xff1b; 结果这个时候&#xff0c;Google Chrome就直接在工作区2打开&#xff0c;多不爽&#xff1f;&#xff01; &…

oracle 插入含字符串

1、创建表 SQL> create table t(id number,name varchar2(20)); 表已创建。 2、常规方式插入 SQL> insert into t values(1,’a&b’); 输入 b 的值: a&b 原值 1: insert into t values(1,’a&b’) 新值 1: insert into t values(1,’aa&b’) 已创建 1 行…

认识Makefile文件

以下内容源于C语言中文网的学习与整理&#xff0c;非原创&#xff0c;如有侵权请告知删除。 一、Makefile文件是什么 Makefile 文件描述了 Linux 系统下 C/C 工程文件的编译规则&#xff0c;比如某些文件是否需要编译、文件编译的顺序、文件间的依赖关系、文件是否需要重建等等…

搭建Spring MVC 4开发环境八步走

Spring MVC作为SpringFrameWork的产品&#xff0c;自诞生之日&#xff0c;就受到广泛开发者的关注&#xff0c;如今Spring MVC在Java中的发展可谓是蒸蒸日上&#xff0c;如今如果再有开发者说&#xff0c;不了解Spring MVC&#xff0c;或许就被人笑掉大牙。煽情的话就不说了&am…

JQ获取CKeditor的值

var editor CKEDITOR.replace("content"); editor.setData(""); alert(CKEDITOR.instances.content.getData()); var editor CKEDITOR.replace("content"); alert(editor.getData()); 转载于:https://www.cnblogs.com/Alandre/p/3405363.htm…

Makefile中变量的定义、引用与赋值

以下内容源于C语言中文网的学习与整理&#xff0c;非原创&#xff0c;如有侵权请告知删除。 Makefile文件中的变量有很多种类&#xff0c;其意义各不相同。比如普通变量、环境变量&#xff0c;自动变量&#xff0c;模式指定变量等。 这里主要讲普通变量的定义与使用。 一、变量…