Search email in the database through the name

2

Ihaveallclientsinsertedinlistbox1butIforexamplejustwanttoselectsome,andthosegotolistbox2.

Inlistbox2Ihavetheidandthenameoftheclientsand,bypressingthe"send email" button, I need to automatically fetch the emails from them.

private void button5_Click(object sender, EventArgs e)
{
    SmtpClient cliente = new SmtpClient();
    MailMessage msg = new MailMessage();
    System.Net.NetworkCredential smtpCreds = new System.Net.NetworkCredential("meu mail", "Minha pass");

    try
    {
        cliente.Host = "smtp.gmail.com";
        cliente.Port = 587;
        cliente.UseDefaultCredentials = false;
        cliente.Credentials = smtpCreds;
        cliente.EnableSsl = true;

        string body = string.Concat("Nome: ", txtnome.Text, "\nE-Mail:", txtemail.Text, "\nMensagem", txtmsg.Text);
        msg.Subject = "fale connosco";
        msg.Body = body;
        msg.From = new MailAddress("MEU EMAIL");
        msg.To.Add(new MailAddress("AJUDA!"));
        cliente.Send(msg);

        label6.Text = "E-mail enviado com sucesso!";
    }
    catch
    {
        label6.Text = "Erro ao enviar E-mail";
    }
}

IgotthankstoafriendandsoIleavetheanswerhere

intnumclientes=listBox2.Items.Count;for(inti=0;i<numclientes;i++){stringdestinatario=listBox2.Items[i].ToString();string[]words=destinatario.Split('-');StringQuery="SELECT email FROM cliente where Cod_Cliente=" + words[0];
                SqlCommand cmdDataBase = new SqlCommand(Query, cn);
                SqlDataReader myreader;

                try
                {

                    cn.Open();
                    myreader = cmdDataBase.ExecuteReader();

                    while (myreader.Read())
                    {
                        msg.To.Add(new MailAddress(myreader.GetString(0)));
                        cliente.Send(msg);
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                cn.Close();
    
asked by anonymous 13.05.2015 / 16:08

1 answer

0

Only formatting the response already provided in question with some modifications, such as opening and closing the connection out of for :

int numclientes = listBox2.Items.Count;
cn.Open();
for (int i = 0; i < numclientes; i++)
    {
        string destinatario = listBox2.Items[i].ToString();
        string[] words = destinatario.Split('-');


        String Query = "SELECT email FROM cliente where Cod_Cliente=" + words[0];
        SqlCommand cmdDataBase = new SqlCommand(Query, cn);
        SqlDataReader myreader;

        try
        {
            myreader = cmdDataBase.ExecuteReader();

            while (myreader.Read())
            {
                msg.To.Add(new MailAddress(myreader.GetString(0)));
                cliente.Send(msg);
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

cn.Close();
    
23.05.2015 / 22:08