Curtis
Curtis

Reputation: 103338

Check that email address is valid for System.Net.Mail.MailAddress

Currently, to avoid errors from being thrown up due to invalid email addresses, I do the following:

Dim mailAddress As MailAddress
Try
   mailAddress = New MailAddress("testing@[email protected]")
Catch ex As Exception
   'Invalid email
End Try

However, rather than depending on Try..Catch, is there a way of validating that the email address will be 100% valid for the MailAddress type?

I know there a plenty of regex functions out there for validating emails, but I'm looking for the function which the MailAddress type uses to validate its addresses.

Upvotes: 38

Views: 37624

Answers (6)

seekingtheoptimal
seekingtheoptimal

Reputation: 719

Recently the .NET API was extended with a MailAddress.TryCreate method, probably coming in future releases, which will eliminate the need for the common try-catch boilerplate: https://github.com/dotnet/runtime/commit/aea45f4e75d1cdbbfc60daae782d1cfeb700be02

Upvotes: 8

Patrick
Patrick

Reputation: 8300

MS also provide the code for a regex based email validator: https://msdn.microsoft.com/en-us/library/01escwtf%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Upvotes: 1

Walter Stabosz
Walter Stabosz

Reputation: 7735

Not really an answer to this question per se, but in case anyone needs it, I wrote a C# function for validating email addresses using this method.

FixEmailAddress("[email protected]")

returns "[email protected]"

FixEmailAddress("wa@[email protected],[email protected],asdfdsf,[email protected]")

returns "[email protected],[email protected]"

I process lists of email addresses this way because a comma separated list of emails is a valid parameter for MailAddressCollection.Add()

/// <summary>
/// Given a single email address, return the email address if it is valid, or empty string if invalid.
/// or given a comma delimited list of email addresses, return the a comma list of valid email addresses from the original list.
/// </summary>
/// <param name="emailAddess"></param>
/// <returns>Validated email address(es)</returns>  
public static string FixEmailAddress(string emailAddress)
{

   string result = "";

    emailAddress = emailAddress.Replace(";",",");
   if (emailAddress.Contains(","))
   {
       List<string> results = new List<string>();
       string[] emailAddresses = emailAddress.Split(new char[] { ',' });
       foreach (string e in emailAddresses)
       {
           string temp = FixEmailAddress(e);
           if (temp != "")
           {
               results.Add(temp);
           }
       }
       result = string.Join(",", results);
   }
   else
   {

       try
       {
           System.Net.Mail.MailAddress email = new System.Net.Mail.MailAddress(emailAddress);
           result = email.Address;
       }
       catch (Exception)
       {
           result = "";
       }

   }

   return result;

}

Upvotes: 1

user898296
user898296

Reputation:

If you need to make sure a given email address is valid according to the IETF standards - which the MailAddress class seems to follow only partially, at the time of this writing - I suggest you to take a look at EmailVerify.NET, a .NET component you can easily integrate in your solutions. It does not depend on regular expressions to perform its job but it relies on an internal finite state machine, so it is very very fast.

Disclaimer: I am the lead developer of this component.

Upvotes: 5

NaveenBhat
NaveenBhat

Reputation: 3318

Some characters are valid in some service providers but the same is not in others! The SmtpClient don't know anything about the service providers. So it has to filter as least as possible. The Wikipedia is welly mentioned about the standers.

Validation of MailAddress is mentioned on the MSDN. Hence I think you can check for those validations before initializing the MailAddress.

Upvotes: 0

SLaks
SLaks

Reputation: 887195

Unfortunately, there is no MailAddress.TryParse method.

Your code is the ideal way to validate email addresses in .Net.

Upvotes: 41

Related Questions