0

I have string coming in this format as shown bellow:

"[email protected];[email protected];[email protected];fault@mail"

What would be the most efficient way to validate each of these above and fail if it is not valid e-mail?

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
bane 975
  • 87
  • 1
  • 7

4 Answers4

2

you can use EmailAddressAttribute class of System.ComponentModel.DataAnnotations namespace for validating the email address. Before that you need to split up individual mails and check whether it is valid or not. the following code will help you to collect the valid mails and invalid mails seperately.

List<string> inputMails = "[email protected];[email protected];[email protected];fault@mail".Split(';').ToList();
List<string> validMails = new List<string>();
List<string> inValidMails = new List<string>();
var validator = new EmailAddressAttribute();
foreach (var mail in inputMails)
{
    if (validator.IsValid(mail))
    {
        validMails.Add(mail);
    }

    else
    {
        inValidMails.Add(mail);
    }
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

You can use Regex or you might split the string by ';' and try to create a System.Net.Mail.MailAddress instance for each and every address. FormatException will occur if address is not in a recognized format.

Gabor
  • 3,021
  • 1
  • 11
  • 20
0

If you're sure, that all e-mails are semi colon separated, you can split it and make a list of all. The best way for me to validate each e-mail is to use a regex pattern. I've used this one:

        var emailPattern = @"(?=^.{1,64}@)^[a-zA-Z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&amp;'*+/=?^_`{|}~-]+)*@(?=.{1,255}$|.{1,255};)(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])(;(?=.{1,64}@)[a-zA-Z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&amp;'*+/=?^_`{|}~-]+)*@(?=.{1,255}$|.{1,255};)(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9]))*$";
        var incomingString = "[email protected];[email protected];[email protected];fault@mail";
        var emails = incomingString.Split(';').ToList();

        foreach (var email in emails)
        {
            if (new Regex(emailPattern).IsMatch(email))
            {
                // your logic here
            }
        }
Tanya Petkova
  • 155
  • 1
  • 1
  • 7
0

Since .Net has out of the box ways to validate an email id, I would not use a regex and rely upon .Net. e.g the EmailAddressAttribute from System.ComponentModel.DataAnnotations. A clean way to use it would be something like:

var emailAddressAttribute = new EmailAddressAttribute();
var groups = yourEmailsString.Split(new [] { ';' }, StringSplitOptions.RemoveEmptyEntries) 
                             .GroupBy(emailAddressAttribute.IsValid);

This will give you 2 groups, the one with the Key == true will be valid email ids

var validEmailIds = groups.Where(group => group.Key)
                        .SelectMany(group => group);

the one with Key == false will be invalid email ids

var invalidEmailIds = groups.Where(group => !group.Key)
                        .SelectMany(group => group);

You could also run up a for loop after grouping, according to your needs..

bit
  • 4,407
  • 1
  • 28
  • 50