I want to send an email to more than 2 people using my own database. So I use for loop to send an email. But it is just sent it to a first person. I also check that the email_data has more than 2, and I bring the email data well. Here is my code.
private void Email()
{
DataTable email_data = GetEmailData();
.....
String from = "aa@gmail.com";
for (int i = 0; i <= email_data.Rows.Count; i++)
{
String to = email_data.Rows[i][0].ToString();
using (MailMessage mm = new MailMessage(from, to))
{
SmtpClient smtp = new SmtpClient();
mm.Subject = "List";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "List.xlsx"));
mm.IsBodyHtml = true;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
credentials.UserName = "aa@gmail.com";
credentials.Password = "aa";
smtp.UseDefaultCredentials = true;
smtp.Credentials = credentials;
smtp.Port = 587;
//works well in the first loop, but stops here in the second loop
smtp.Send(mm);
}
}
}