日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/18 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个基于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

總結

以上是生活随笔為你收集整理的一个基于POP3协议进行邮箱账号验证的类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。