[英]Why does MailAddress not reject the email address someName@gmail?
[英]Why does MailAddress think 'john@gmail.' is a valid email address?
像一个优秀的 C# 用户一样,我使用MailAddress
对象来验证电子邮件地址。
我的一个客户输入了john@gmail.
他的电子邮件,经过MailAddress
验证,破坏了我的软件。 我希望下面的代码抛出异常,但事实并非如此。
static void Main(string[] args)
{
string addressmail = string.Empty;
try
{
MailAddress mail = new MailAddress(@"john@gmail.");
addressmail = mail.Address;
}
catch (FormatException)
{
// address is invalid
}
// address is valid
Console.WriteLine(addressmail);
}
你知道如何捕捉这种虚假的邮件地址吗?
MailAddress 类型对验证电子邮件地址的支持非常有限,并且从 .NET 4.0 开始,不支持大多数相关的 IETF 标准。 如果您需要验证电子邮件地址的语法,可能不使用正则表达式,我建议您查看EmailVerify.NET ,这是一个支持有关该主题的所有当前标准的 .NET 组件(RFC 1123、RFC 2821 、RFC 2822、RFC 3696、RFC 4291、RFC 5321 和 RFC 5322)。 如果需要,该组件甚至允许对地址执行其他测试,包括 DNS、SMTP 和邮箱检查。
免责声明:我是该产品的首席开发人员。
MailboxValidator 有一个您可以使用的免费 API。 只需要在http://www.mailboxvalidator.com/plans#api上注册免费的 API 计划,那么集成部分就很容易了,因为他们还有一个 C# 类http://www.mailboxvalidator.com/dotnet你来包装 API 调用。
C# 类代码位于https://github.com/MailboxValidator/mailboxvalidator-csharp
要通过 NuGet ( https://www.nuget.org/packages/MailboxValidator.SingleValidation/ ) 安装 MailboxValidator SingleValidation 类, 请在包管理器控制台中运行以下命令:
Install-Package MailboxValidator.SingleValidation
然后你可以像下面这样使用这个类:
using System;
using System.Windows.Forms;
using MailboxValidator;
namespace TestMailboxValidatorCSharp
{
public class TestMailboxValidatorCSharp
{
static void Main(string[] args)
{
var mbv = new SingleValidation("PASTE_YOUR_API_KEY_HERE");
String results = "";
try
{
MBVResult rec = mbv.ValidateEmail("example@example.com");
if (rec.ErrorCode == "")
{
results += "email_address: " + rec.EmailAddress + "\n";
results += "domain: " + rec.Domain + "\n";
results += "is_free: " + rec.IsFree + "\n";
results += "is_syntax: " + rec.IsSyntax + "\n";
results += "is_domain: " + rec.IsDomain + "\n";
results += "is_smtp: " + rec.IsSMTP + "\n";
results += "is_verified: " + rec.IsVerified + "\n";
results += "is_server_down: " + rec.IsServerDown + "\n";
results += "is_greylisted: " + rec.IsGreylisted + "\n";
results += "is_disposable: " + rec.IsDisposable + "\n";
results += "is_suppressed: " + rec.IsSuppressed + "\n";
results += "is_role: " + rec.IsRole + "\n";
results += "is_high_risk: " + rec.IsHighRisk + "\n";
results += "is_catchall: " + rec.IsCatchall + "\n";
results += "mailboxvalidator_score: " + rec.MailboxValidatorScore + "\n";
results += "time_taken: " + rec.TimeTaken + "\n";
results += "status: " + rec.Status + "\n";
results += "credits_available: " + rec.CreditsAvailable + "\n";
}
else
{
results += "error_code: " + rec.ErrorCode + "\n";
results += "error_message: " + rec.ErrorMessage + "\n";
}
results += "version: " + rec.Version + "\n";
MessageBox.Show(results);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n" + ex.StackTrace);
}
}
}
}
以某种方式固定版本,混合 MailAddress 和一个简单的 Regex 来验证主机:
SLaks 建议的静态正则表达式
private static readonly Regex hostReg = new Regex(@"(\w+)\.(\w+)");
public bool IsMailAddress(string addParam)
{
try
{
MailAddress mail = new MailAddress(addParam);
string address = mail.Address;
//not handled by MailAdress, which is a shame
return hostReg.IsMatch(mail.Host);
}
catch (FormatException)
{
//address is invalid
return false;
}
catch (Exception)
{
return false;
}
}
MailAddress 尝试与废弃 RFC822 的 RFC2822 兼容。 当您阅读 MailAddress 的源代码时,您会看到接受它的结尾点只是为了与某些电子邮件客户端兼容。 使用正则表达式验证电子邮件地址不是正确的做法(参见 RFC2822),最好的方法是实现解析器,MailAddress 所做的。
不是免费的解决方案,但Cobisi 的电子邮件验证库可以在不同的准确度(语法、IspSpecificSyntax、DeaDomain、Dns、DeaMailExchanger、Smtp、Mailbox、CatchAll)内判断电子邮件是否有效
var engine = new VerificationEngine();
var result = engine.Run("john@example.com",
VerificationLevel.Mailbox).Result;
if (result.LastStatus == VerificationStatus.Success)
{
// TODO: Show a message box with the great news
}
免责声明:我与公司或项目无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.