Not all e-mails being sent using SmtpClient in an ASP.NET MVC getting delivered. Why? How to resolve?

StackOverflow https://stackoverflow.com/questions/2275547

  •  21-09-2019
  •  | 
  •  

Question

Here is the set-up:

  1. I have a Notifications controller that is called from task scheduler 1x/day
  2. The action method pulls upwards of 300 addresses, loops thru them and uses the SmtpClient class to send an individual e-mail to each recepient.

From what I can tell the process runs fine with no exceptions ... except that not all e-mails are being delivered. Anyone have any ideas on what is going on and how to resolve?

Here is the code:

foreach (var emp in division.Users)
{
    var fromAddress = "myfromaddress";
    var mailServer = "mymailserver";

    var toEmail = emp.EmailAddress;

    var message = new MailMessage(fromAddress, toEmail)
                    {
                        Subject = subject,
                        Body = "<body style='font:normal 13px tahoma,arial,helvetica;'>" + body + "</body>",
                        IsBodyHtml = true
                    };

    var client = new SmtpClient(mailServer);
    client.Send(message);
}

UPDATE:

Adding a pause in between sending e-mails resolves the problem. But why does this work? And is there a better way (e.g. using Async()) that would equally resolve the issue in a better way???

Updated code ...

foreach (var emp in division.Users)
{
    var fromAddress = "myfromaddress";
    var mailServer = "mymailserver";

    var toEmail = emp.EmailAddress;

    var message = new MailMessage(fromAddress, toEmail)
                    {
                        Subject = subject,
                        Body = "<body style='font:normal 13px tahoma,arial,helvetica;'>" + body + "</body>",
                        IsBodyHtml = true
                    };

    var client = new SmtpClient(mailServer);
    client.Send(message);

    Thread.Sleep(3000); // Wait 3s until sending next message
}
Was it helpful?

Solution

there is a default timeout with the smtp client and default value is 100sec

more info here

OTHER TIPS

If you are not having any exceptions I'd check SPAM folders and email addresses. I'd also try sending an email manually from your outlook to one of the addresses that didn't recieve a message.

On a side note, unless you are using different mail servers, I think you can change this code to

var client = new SmtpClient(mailServer); 
var mailServer = "mymailserver";

foreach (var emp in division.Users) 
{ 
    var fromAddress = "myfromaddress"; 


    var toEmail = emp.EmailAddress; 

    var message = new MailMessage(fromAddress, toEmail) 
                    { 
                        Subject = subject, 
                        Body = "<body style='font:normal 13px tahoma,arial,helvetica;'>" + body + "</body>", 
                        IsBodyHtml = true 
                    }; 


    client.Send(message); 
} 

You might also try SendAsync method of the SmtpClient class, like this:

// setup the callback method when the send finishes
client.SendCompleted += SendComplete; //new SendCompletedEventHandler(smtpSender_SendCompleted);

// send the email
client.SendAsync(message, null);



private void SendComplete(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    // do stuff on complete
}

I would strongly advice on using SmtpClient.SendAsync() as Send() is blocking.

I would check the logs of the SMTP server that you're sending to. Note: sending your own emails are more likely to end in Junk mail than using a trusted provider.

Edit: added sample SendAsync code

smtpClient.SendCompleted += smtpClient_SendCompleted;

static void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    if (e.Error != null)
    {
        Log.ErrorFormat("Async Mail exception {0} :: {1}", 
            e.Error.GetType().Name, e.Error.Message);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top