-1

I am sending emails using c# using SmtpClient. I have to send aproximately one hundred different emails per day, and I can't use the same mail (adding several recipients) since the email changes according to the recipient.

I am not using a local SMTP server, and I understand (according to @rizzle response here) that some time has to be left between one mail and another one. However, I am sleeping my program for 10 seconds and still, it is only the first email that gets sent, never the second one (so far I am trying my system with two emails instead of one hundred). This is my code, any ideas?

foreach (Person p in clientList)
            {
                AlternateView plainView = AlternateView.CreateAlternateViewFromString("Texto visible para clientes que no tienen HTML", null, "text/plain");
                //AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:companylogo>", null, "text/html");

                string htmlString = "html string body of the email";
                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlString, null, "text/html");

                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                message.AlternateViews.Add(htmlView);
                message.To.Add(p.email.Trim());
                message.Subject = p.nombre+", email subject";
                message.From = new System.Net.Mail.MailAddress(fromAddress);
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("...");
                NetworkCredential myCreds = new NetworkCredential(usr, pass, "");
                client.Credentials = myCreds;
                client.Send(message);
                System.Threading.Thread.Sleep(10000);

            }
Community
  • 1
  • 1
Soph
  • 2,895
  • 5
  • 37
  • 68
  • How long do you have to sleep before 2 can be sent? – Bali C Nov 10 '11 at 12:38
  • Could you include some more code, such as the loop you are using the send within - and any error messages you are getting – Andrew Nov 10 '11 at 12:39
  • @Andrew I just included the complete loop code. – Soph Nov 10 '11 at 12:51
  • @Bali C , not sure I quite get what you're asking... I haven't been able to send both emails, tried sleeping for 10s and since that didn't work, I realized there had to be another workaround for this. – Soph Nov 10 '11 at 12:51
  • @Soph I just meant would it allow you to send both emails after say 30seconds or a minute, just to debug it a bit, but the answers below look like they might be what your after. – Bali C Nov 10 '11 at 13:11
  • @Soph I don't know why you have -3, its a good question! +1 – Bali C Nov 10 '11 at 13:12
  • @BaliC I was wondering the exact same thing! Thanks for the upvote! – Soph Nov 10 '11 at 13:21

2 Answers2

4

Do client.Dispose() right after you send your last message, if you're on dotnet 4. This will force the dotnet SMTP stuff to finish its work.

See here: System.Net.Mail and MailMessage not Sending Messages Immediately

If you're on earlier versions of DotNet, try doing a couple of things.

Do message.Dispose() when you're done with the message instance.

Define your SmtpClient locally (within a method) and exit the method when you're done sending. That is, don't try to keep your client instance around as a field in one of your long-lived class instances; it won't flush the last message you sent to the server until it's finalized.

(They really did fix this in dotnet 4.0)

Community
  • 1
  • 1
O. Jones
  • 103,626
  • 17
  • 118
  • 172
2

In 3.5 try using:

SmtpClient client = ...

client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

using (MailMessage message = ...)
{
  // where userToken is a user-defined token
  // to be passed to the SendCompletedCallback
  client.SendAsync(message, userToken);
} // Disposes of message

Then in the SendCompletedCallback trigger the next...

SmtpClient Class

Paul
  • 4,009
  • 1
  • 17
  • 15
  • thanks for the suggestion, however I re targeted my project to dotnet 4 and did what Ollie jones suggested. Thanks anyway! – Soph Nov 10 '11 at 13:16