1

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);
                    }
                }
        }
Scarlett
  • 173
  • 12
  • Although it is probably not cause of your problem, please note that `SmtpClient` and `Attachment` implements `IDisposable` so you should enclose them in `using(...)` statement – Ňuf Jun 05 '17 at 17:09
  • @Nuf I think I use using(...) statement, you mean for loop also should be enclosed by using() statement? – Scarlett Jun 05 '17 at 17:18
  • I mean `using(SmtpClient smtp = new SmtpClient()) {...}` and `using(Attachment(new MemoryStream(bytes), "List.xlsx")) {...}` – Ňuf Jun 05 '17 at 17:21
  • @Nuf I try that but still doesn't work.. For the first email, it is sent perfect, but when I go to 2nd for loop , it just stopped before smtp.Send(mm). – Scarlett Jun 05 '17 at 19:23

2 Answers2

0

check the row count by adding debug prints

And also try putting sleep at the end

                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = credentials;
                    smtp.Port = 587;
                    smtp.Send(mm);

System.Threading.Sleep(1000);

0

I tried to run your code on my computer (I just replaced credentials with my private gmail account credentials) and it worked without any problem - emails was successfully sent to 3 different email addresses. So problem might be related to your computer. So here are a few ideas what you can try:

  • run your program on different computer to see if it works there (for comparison, I run it successfully on Windows 10 Home, Visual Studio 2017 CE, Console Application targeting .NET Framework 4.7)
  • check your antivirus and firewall if they are not somehow blocking mass email sending.
  • try to debug source code of SmtpClient.Send() method to see where exactly execution stops, which should give you a hint what is wrong and how to fix it.
  • in app.config enable detailed logging of network communication and see if there isn't some problem when connecting to SMTP server.

BTW there is probably bug: for (int i = 0; i <= email_data.Rows.Count; i++) should be for (int i = 0; i < email_data.Rows.Count; i++)

Ňuf
  • 6,027
  • 2
  • 23
  • 26
  • That's so strange...It still doesn't work I decide to use BCC instead of those. anyway thank you..! – Scarlett Jun 07 '17 at 16:29