[C#] 纯文本查看 复制代码
// ===============================================================================
// 老陈出击,必属精品!
//
// Copyright © ymind.net. All rights reserved .
// 版权所有:彦铭
// ===============================================================================
using System;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;
namespace WindowsFormsApplication1
{
/// <summary>
/// 提供通过 POP3 协议进行电子信箱账号验证的功能。
/// </summary>
public sealed class POP3AccountValidator
{
#region ValidateResults enum
/// <summary>
/// 表示验证结果的枚举值。
/// </summary>
public enum ValidateResults
{
/// <summary>
/// 未指定。
/// </summary>
None = 0,
/// <summary>
/// 连接失败。
/// </summary>
ConnectFailed = 1,
/// <summary>
/// 无效的登录账号。
/// </summary>
InvalidUserName = 2,
/// <summary>
/// 无效的登录密码。
/// </summary>
InvalidPassword = 3,
/// <summary>
/// 登录成功。
/// </summary>
Success = 4,
/// <summary>
/// 验证过程发生异常。
/// </summary>
Error = 5,
}
#endregion
private const string _CRLF = "\r\n";
private readonly bool _useSSL;
/// <summary>
/// 初始化 <see cref="POP3AccountValidator"/> 类的新实例。
/// </summary>
/// <param name="server">指定 POP3 服务器。</param>
public POP3AccountValidator(string server) : this(server, 110) { }
/// <summary>
/// 初始化 <see cref="POP3AccountValidator"/> 类的新实例。
/// </summary>
/// <param name="server">指定 POP3 服务器。</param>
/// <param name="port">指定 POP3 服务器端口号。</param>
public POP3AccountValidator(string server, int port) : this(server, port, false) { }
/// <summary>
/// 初始化 <see cref="POP3AccountValidator"/> 类的新实例。
/// </summary>
/// <param name="server">指定 POP3 服务器。</param>
/// <param name="port">指定 POP3 服务器端口号。</param>
/// <param name="useSSL">指定一个值,该值指示验证过程是否使用 SSL 加密协议。</param>
public POP3AccountValidator(string server, int port, bool useSSL)
{
if (String.IsNullOrWhiteSpace(server)) throw new ArgumentOutOfRangeException("server");
if (port < 1 || port > 65535) throw new ArgumentOutOfRangeException("port");
this.Server = server;
this.Port = port;
this._useSSL = useSSL;
}
/// <summary>
/// 获取 POP3 服务器。
/// </summary>
public string Server { get; private set; }
/// <summary>
/// 获取 POP3 服务器端口号。
/// </summary>
public int Port { get; private set; }
private static ValidateResults _Validate(Stream stream, string username, string password)
{
var data = "USER " + username + _CRLF;
using (var reader = new StreamReader(stream))
{
if (!reader.ReadLine().Contains("+OK")) return ValidateResults.ConnectFailed;
var charData = Encoding.ASCII.GetBytes(data);
stream.Write(charData, 0, charData.Length);
if (!reader.ReadLine().Contains("+OK")) return ValidateResults.InvalidUserName;
data = "PASS " + password + _CRLF;
charData = Encoding.ASCII.GetBytes(data);
stream.Write(charData, 0, charData.Length);
return reader.ReadLine().Contains("+OK") ? ValidateResults.Success : ValidateResults.InvalidPassword;
}
}
/// <summary>
/// 验证电子信箱账号。
/// </summary>
/// <param name="username">电子信箱账号。</param>
/// <param name="password">电子信箱密码。</param>
/// <returns>返回 <see cref="ValidateResults"/> 枚举值之一。</returns>
public ValidateResults Validate(string username, string password)
{
if (username == null) throw new ArgumentNullException("username");
if (password == null) throw new ArgumentNullException("password");
try
{
using (var tcpClient = new TcpClient(this.Server, this.Port))
{
using (var tcpStream = tcpClient.GetStream())
{
if (!this._useSSL) return _Validate(tcpStream, username, password);
using (var sslStream = new SslStream(tcpStream, false))
{
sslStream.AuthenticateAsClient(this.Server);
return _Validate(sslStream, username, password);
}
}
}
}
catch
{
return ValidateResults.Error;
}
}
}
}