Ihaveallclientsinsertedinlistbox1
butIforexamplejustwanttoselectsome,andthosegotolistbox2
.
Inlistbox2
Ihavetheidandthenameoftheclientsand,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();