一個基於POP3協議進行郵箱帳號驗證的類

最近老陳要針對企業郵箱作一些開發,以對接企業OA神馬的,但企業郵箱惟獨沒有開放帳號密碼驗證功能,很惱火!不得已,翻出早些年的Asp代碼改編成了C#類,實現了一個C#下的經過POP3協議進行郵箱帳號驗證的類,並且還能完美支持SSL加密,貌似很實用的樣子,分享給你們先!服務器

無廢話,直接放代碼:tcp

  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 }
相關文章
相關標籤/搜索