Remove point and comma character and spaces from the last email in the recipient list

Asked

Viewed 156 times

0

Hello, I want to send my emails as below:

[email protected];[email protected];

but it’s giving me a sending error, because it’s taking the semicolon of the last address, when I put the emails in this way [email protected];[email protected] without the ; I can send normally, but if the user type the ; I need to do a treatment for this, which is what I’m not getting.

I’ll leave the code section below, from what I’ve tried to do, thank you.

mail.From = new MailAddress(email);
//EmailIsValid(destinatario);
//mail.To.Add(destinatario);
string[] multiplesSend = destinatario.Split(';');
foreach (var emails in multiplesSend)
{
     string expression = "^[A-Za-z0-9\\._%-]+@[A-Za-z0-9\\.-]+\\.[A-Za-z]{2,4}(?:[;][A-Za-z0-9\\._%-]+@[A-Za-z0-9\\.-]+\\.[A-Za-z]{2,4}?)*";
     if (Regex.IsMatch(destinatario, expression))
           mail.To.Add(emails);
     }
}

1 answer

3


I think the problem in these cases is that the Split creates an empty field in the array, since you have a ; at the end of the string.

To solve this you can add an argument to Split to remove empty strings in the list

string[] multiplesSend = destinatario.Split(new char[] { ';' }, 
    StringSplitOptions.RemoveEmptyEntries);

or check if the email is empty before adding

if (!string.IsNullOrEmpty(emails) && Regex.IsMatch(destinatario, expression))
    mail.To.Add(emails);
  • Diogo any questions about formatting see the link: https://answall.com/editing-help it is of utmost importance that you have this knowledge being a new user.

  • 1

    @Virgilionovic Thanks for the information!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.