How to wait for the validation of the while loop to complete before the next code execution

0

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
    
asked by A arancibia 08.02.2017 в 20:12
source

2 answers

1

If I'm not wrong (and do not use some code that does not show in some undocumented method that modifies userIDindex ) you probably do not want to use while , but if .

More than anything, if as you say, it "works well, just send the emails twice", it is sure that you change something in ToString or Equals .

In a loop while you should have a value that changes the condition, if you do not have a while in most cases not very useful, one that never runs or always. For that you would not need so much code in the condition, a while(true) (always runs) or a while(false) (it never runs) would be more obvious.

    
answered by 08.02.2017 в 20:44
0

You create a function called Sendmail

  

This Boolean Function Receives the recipient, subject, body and copy

     public bool EnviarCorreo(string To, string Asunto, string Cuerpo, string copiado)
    {
        bool EnviadoCorrectamente = true;
        try
        {
            string smtpAddress = "smtp.gmail.com";
            int portNumber = 587;
            bool enableSSL = true;
            string emailTo = To;
            using (MailMessage mail2 = new MailMessage())
            {
                mail2.From = new MailAddress("tumail");
                mail2.To.Add(emailTo);
                if (copiado != "") { mail2.CC.Add(copiado); }
                mail2.Subject = Asunto;
                mail2.Body = Cuerpo;
                mail2.IsBodyHtml = true;
                using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                {
                    smtp.Credentials = new NetworkCredential("tumail", "tupass");
                    smtp.EnableSsl = enableSSL;
                    smtp.Send(mail2);
                }
            }
        }
        catch { EnviadoCorrectamente = false; }
        return EnviadoCorrectamente;
    }

Afterwards you only send her to call

if(userData[userIDindex].ToString().Equals("3") || userData[userIDindex].ToString().Equals("4"))
 {
 bool procesoenv = EnviarCorreo(mail_rest, subject, body, label1.Text);
  if (procesoenv)
    {
    MessageBox.Show("CORRECTAMENTE ENVIADO");
    }
    else
     {
      MessageBox.Show("Ha habido un error al enviar el correo");                      
     }
 }
 //Termina el Proceso
    
answered by 08.02.2017 в 20:29