一个基于POP3协议进行邮箱账号验证的类_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 一个基于POP3协议进行邮箱账号验证的类

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

 2013/10/23 17:55:28  陈彦铭  博客园  我要评论(0)
  • 摘要:最近老陈要针对企业邮箱做一些开发,以对接企业OA神马的,但企业邮箱唯独没有开放账号密码验证功能,很恼火!不得已,翻出早些年的Asp代码改编成了C#类,实现了一个C#下的通过POP3协议进行邮箱账号验证的类,而且还能完美支持SSL加密,貌似很实用的样子,分享给大家先!无废话,直接放代码:1//===============================================================================2//老陈出击,必属精品
  • 标签:一个 协议

最近老陈要针对企业邮箱做一些开发,以对接企业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 }

 

发表评论
用户名: 匿名