2
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";
}
}
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();
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?
– Vinícius Gobbo A. de Oliveira
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.
– Marcus Vinicius
help print the tables?
– Fabio Gonçalves
@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?
– Leonel Sanches da Silva
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
– Fabio Gonçalves
You want for example
Nome = Joao
, search the database for the emailJoao
? Already trying to do something? How is theEstrutura
of the table that is storing theemails
?– MeuChapeu
Thanks to everyone who tried to help but I’ve already solved the problem :D
– Fabio Gonçalves
Hello mortal @Fabiogonçalves, has how to put the answer so that future mortals with the same problem can solve them too?
– PauloHDSousa
Hello Fabio, put your answer as an answer and not in the question itself. So everything is straight.
– Raimundo Norberto