Remove semicolon character and spaces from last email from recipients list
Hello, I want to send my emails as below:
[email protected];[email protected];
But it is giving me sending error, since it is 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 enters the ;
I need to do a treatment for it, which is what I am not getting.
I will leave the code snippet below, than I have already 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);
}
}
0
1 answers
I think the problem in these cases is that Split
creates an empty field in array
, since you have a ;
at the end of the string.
To solve this you can add an argument to Split
to remove the strings that are empty 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);
3
Author: Diogo Magro, 2020-01-07 17:55:36