I have a program in which when you validate a condition in the while loop, if it is true it must send emails.
This works fine, but what I want is that once the validation of the while is finished, I can just execute the code for sending emails.
At this moment it works well for me, but when executed immediately, the emails are sent to me in duplicates each time the while loop is executed, therefore I receive many emails instead of receiving only one.
How can I do this?
Here is an example of my code:
while ((userData[userIDindex].ToString().Equals("3", StringComparison.CurrentCultureIgnoreCase) ||
userData[userIDindex].ToString().Equals("4", StringComparison.CurrentCultureIgnoreCase)))
{
toEmailAdresses.Add(xml.GetElementsByTagName("toEmailAdresses")[t].InnerText);
//apiKey.Add(xml.GetElementsByTagName("apiKey")[i].InnerText);
fromEmailAddress = xml.GetElementsByTagName("fromEmailAddress")[t].InnerText;
bodyMessage = xml.GetElementsByTagName("bodyMessage")[t].InnerText;
//(String.Join(",", fromEmailAddress[t].ToArray())));
MailMessage mail = new MailMessage(fromEmailAddress, toEmailAdresses[t].ToString());
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "localhost"; //here goes the smpt connection
mail.Subject = "Testing email";
mail.Body = bodyMessage;
client.Send(mail);
} //end of the while