编程电子邮件验证

使用 Aspose.Email.Verifications

Aspose.Email.Verifications 提供了对电子邮件地址进行完整验证的全方位支持。借助 Aspose.Email.Verifications.EmailValidator 类,可以执行包括电子邮件语法检查、电子邮件域名检查以及使用邮件服务器检查用户帐户在内的多种验证类型。ValidationPolicy 枚举用于设置验证策略级别:

  • SyntaxOnly 验证电子邮件地址的语法。
  • SyntaxAndDomain 先验证电子邮件地址的语法,然后验证域名。
  • MailServer 通过尝试连接邮件服务器来验证地址。

示例应用程序

Aspose.Email.Verifications 是一个强大且实用的组件,用于检查电子邮件地址、电子邮件域名的有效性以及更多功能。本文展示了如何创建一个使用 Aspose.Email.Verifications 的应用程序。演示应用程序检查电子邮件地址的有效性。

要创建一个使用 Aspose.Email.Verifications 来验证电子邮件地址的应用程序:

  • 打开 Visual Studio。
  • 在 文件 菜单中,点击 新建,然后选择 项目。为简便起见,这将是一个控制台程序。根据需求选择 C# 或 VB.NET 项目。

|创建新项目| | :- | |在 Visual Studio 中创建新项目|

  • 将引用添加到控制台应用程序。

|添加对 Aspose.Email 的引用| | :- | |向 Aspose.Email 添加引用|

  • 在 Aspose.Email 的安装目录下的 Bin 目录中浏览到 Aspose.Email.dll。

以下代码片段演示了如何验证电子邮件地址。代码执行后,会弹出消息提示该地址是否有效。

EmailValidator ev = new EmailValidator();
ValidationResult result;
try
{
    ev.Validate(txtEmailAddr.Text, out result);
    if (result.ReturnCode == ValidationResponseCode.ValidationSuccess)
    {
        lblResult.Text = "The email address is valid.";
    }
    else
    {
        lblResult.Text = "The mail address is invalid,return code is : " + result.ReturnCode + ".";
    }
}
catch (Exception ex)
{
    lblResult.Text += "<br>" + ex.Message;
}
  • 构建并运行。

|运行应用程序|| | :- | :- | |运行电子邮件验证应用程序| |

基本验证功能

使用 Aspose.Email.Verifications 来验证电子邮件地址的有效性。为此,Aspose.Email.Verifications 提供了 EmailValidator 类。

验证电子邮件

Aspose.Email 的验证功能可用于验证电子邮件地址、域名和邮件服务器。下面的代码片段展示了如何使用 EmailValidator 来验证电子邮件地址。

EmailValidator ev = new EmailValidator();
ValidationResult result;
ev.Validate("user@domain.com", out result);
if (result.ReturnCode == ValidationResponseCode.ValidationSuccess)
{
    Console.WriteLine("the email address is valid.");
}
else
{
    Console.WriteLine("the mail address is invalid,for the {0}", result.Message);
}

验证域名

Aspose.Email.Verifications 可以验证域名的有效性。

验证邮件服务器

Aspose.Email.Verifications 可以验证邮件服务器的有效性。

验证电子邮件消息

除了验证地址、域和邮件服务器外,Aspose.Email 还能检查消息 文件 是否结构良好。 MessageValidator 类检查 EML、EMLX、MHT、MSG 或 OFT 文件,并报告其是否符合格式。它提供两个返回 MessageValidationResult:

  • static MessageValidationResult Validate(string fileName)
  • static MessageValidationResult Validate(Stream stream)

结果包含以下成员:

| 成员 | 描述 | | — | — | | IsSuccess | true 当消息通过验证时。 | | FormatType | 检测到的文件格式。 | | ErrorMessage | 验证失败时问题的摘要。 | | Errors | 各个 MessageValidationError 项目。 |

每个错误都会暴露一个 ErrorType —— 一个 MessageValidationErrorType 值(MessageStructureError, EmailAddressError, TransferEncodingError, EmptyMessageClassError 或 UnknownFormat) —— 以及一个 Description 以及,已知的 LineNumber 发现问题的时间点。

MessageValidationResult result = MessageValidator.Validate("message.eml");

Console.WriteLine("Detected format: " + result.FormatType);
Console.WriteLine("Valid: " + result.IsSuccess);

if (!result.IsSuccess)
{
    Console.WriteLine("Summary: " + result.ErrorMessage);

    foreach (MessageValidationError error in result.Errors)
    {
        Console.WriteLine($"{error.ErrorType} (line {error.LineNumber}): {error.Description}");
    }
}

验证也适用于流,这在消息从未触及文件系统时非常方便:

using var stream = File.OpenRead("message.msg");

MessageValidationResult result = MessageValidator.Validate(stream);

Console.WriteLine(result.IsSuccess);