简体   繁体   English

如何使用MailAddress验证硒中的电子邮件并尝试捕获

[英]How to use Validate an email in selenium using MailAddress and Try Catch

I am new to c# so apologise if you see basic errors. 我是C#的新手,所以如果您看到基本错误,我们深表歉意。 I want to validate an email address and I am using a public bool, try catch and then a public void. 我想验证一个电子邮件地址,并且正在使用公共布尔,尝试catch,然后使用公共无效。 I want to retrieve the email text from the relevant HTML which I have been able to do and then validate that it is a valid email using 'MailAddress'. 我想从相关的HTML中检索电子邮件文本,然后使用“ MailAddress”验证它是否为有效电子邮件。

The problem is that it is throwing an exception: 问题在于它引发了一个异常:

  throw new Exception("email within booking confirmation summary is not valid");

I think I am using the code incorrectly but can somebody guide me in what I need to do in the code to ensure I am correctly validating the email as I believe I have some code I don't need. 我认为我使用的代码不正确,但是有人可以指导我执行代码中需要做的事情,以确保我正确地验证了电子邮件,因为我认为我不需要一些代码。 Below is the code: 下面是代码:

 public static bool IsEmail(string emailToValidate)
    {
        if (string.IsNullOrEmpty(emailToValidate))
            return true;

        try
        {
            new MailAddress(emailToValidate);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    public void CheckEmailDisplayed()
    {
        var email = _driver.FindElement(ConfirmationResponsiveElements.ViewEmail);
        var emailText = email.Text;

        if (IsEmail(emailText))
        {
            Console.WriteLine("email is valid");
        }
        else
        {
            throw new Exception("email within booking confirmation summary is not valid");
        }

ViewEmail is pointing to a HTML tag below: ViewEmail指向下面的HTML标签:

 public static By ViewEmail => By.ClassName("confirmation-banner__text");

Thanks 谢谢

Try below :- 请尝试以下:-

bool IsValidEmail(string email)
{
    try
    {
        return new System.Net.Mail.MailAddress(email).Address == email;
    }
    catch
    {
        return false;
    }
}

OR 要么

Use this single liner method - 使用这种单眼线方法-

using System.ComponentModel.DataAnnotations;
public bool IsValidEmail(string source)
{
    return new EmailAddressAttribute().IsValid(source);
}

OR 要么

.net 4.5 added ions.EmaSystem.ComponentModel.DataAnnotatilAddressAttribute .net 4.5添加了ions.EmaSystem.ComponentModel.DataAnnotatilAddressAttribute

You can browse the EmailAddressAttribute's source , this is the Regex it uses internally: 您可以浏览EmailAddressAttribute的源 ,这是它在内部使用的Regex:

const string pattern = @"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$";

OR 要么

public static bool emailIsValid(string email)
{
    string expression = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    if (Regex.IsMatch(email, expression))
    {
        return Regex.Replace(email, expression, string.Empty).Length == 0;
    }
    else
    {
        return false;
    }
}

Hope it will help you :) 希望它能对您有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM