Search email in the database by name

Asked

Viewed 163 times

2

inserir a descrição da imagem aqui

I have all the clients inserted in listbox1 but I just want to select a few, and those go to the listbox2.

In the listbox2 I have the customer id and name, and when I click on the "send email" button, I need you to automatically retrieve the emails from these.

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";
    }
}

inserir a descrição da imagem aqui

I got it thanks to a friend and so I leave here the answer

 int numclientes = listBox2.Items.Count;
            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
                {

                    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();
  • 2

    And where are these emails? In a database? What is the structure of the tables? What database is it? What technology is it using to access?

  • You put the least relevant code to help solve your question. Please show how you are populating your listbox’s and database information as noted above.

  • help print the tables?

  • 1

    @Fabiogonçalves No. We need to know how these emails are recorded. We need an example of how to search these emails in your database. Can you please edit your question again?

  • what do you mean? the emails so recorded in the client table, are then recorded...with the Select email from client query I have access to the emails but what I need is more complex than that

  • You want for example Nome = Joao, search the database for the email Joao? Already trying to do something? How is the Estrutura of the table that is storing the emails?

  • Thanks to everyone who tried to help but I’ve already solved the problem :D

  • Hello mortal @Fabiogonçalves, has how to put the answer so that future mortals with the same problem can solve them too?

  • Hello Fabio, put your answer as an answer and not in the question itself. So everything is straight.

Show 4 more comments

1 answer

0


Just formatting the answer already provided in question with some modifications, such as opening and closing the Connection outside the 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();

Browser other questions tagged

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