17

here is my code

for(int i = 0; i < number ; i++)
{
    MailAddress to = new MailAddress(iMail.to);
    MailAddress from = new MailAddress(iMail.from, iMail.displayName);
    string body = iMail.body;
    string subject = iMail.sub;
    oMail = new MailMessage(from, to);
    oMail.Subject = subject;
    oMail.Body = body;
    oMail.IsBodyHtml = true;
    oMail.Priority = MailPriority.Normal;
    oMail.Sender = from;
    s = new SmtpClient(smtpServer);
    if (s != null)
    {
        s.Send(oMail);
    }
    oMail.Dispose();
    s = null;
}

this loops sends over 60,000 email. but my problem i am getting " failure sending mail" in some of the email some times 5000 and some time less then that rest of them gets delivered. and i have check all those error out email has valid email address. dont know what is the problem. i really need help in this.

Edit: This is my exception Trace

Error - Failure sending mail.; Inner Ex - System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed. at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)

Some guy
  • 479
  • 6
  • 10
Nnp
  • 1,813
  • 7
  • 36
  • 62

8 Answers8

14

Well, the "failure sending e-mail" should hopefully have a bit more detail. But there are a few things that could cause this.

  1. Restrictions on the "From" address. If you are using different from addresses, some could be blocked by your SMTP service from being able to send.
  2. Flood prevention on your SMTP service could be stopping the e-mails from going out.

Regardless if it is one of these or another error, you will want to look at the exception and inner exception to get a bit more detail.

mattruma
  • 16,589
  • 32
  • 107
  • 171
Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
  • Thanks Mitchel, i use same "from" email address for all, but most of them got delivered. that means option (2) left out for check? – Nnp Feb 05 '10 at 19:14
  • Yes, it is very possible. Many SMTP services have an X per Y configuration to throttle. If you capture the full exception, including inner exception, you should be able to get a bit more light on the situation. – Mitchel Sellers Feb 05 '10 at 19:39
  • Thanks Mitchel, i have modified my question with exception trace.it says "net_io_connectionclosed", what does that mean? – Nnp Feb 05 '10 at 19:40
  • Well, typically it is a mis-matched "From" address, but it could be due to something like my #2 item. Basically it was not able to get a valid connection to SMTP. Do you have access to SMTP Logs – Mitchel Sellers Feb 05 '10 at 19:48
  • Thanks Mitchel, i dont have access to SMTP logs :(. i found this http://forums.asp.net/t/924682.aspx , in the last post of this forum somebody says to use lient.ServicePoint.MaxIdleTime = 1; does that make sense? – Nnp Feb 05 '10 at 20:06
  • i tried that, so far no error but that comes with the cost of cpu usage. it keep 20% cpu even there is no email to send – Nnp Feb 05 '10 at 20:44
  • i tried client.ServicePoint.MaxIdleTime = 1 that does not work, then i tried client.ServicePoint.ConnectionLeaseTimeout = 0 even that does not work. i am stuck please help. – Nnp Feb 08 '10 at 22:03
4

apparently this problem got solved just by increasing queue size on my 3rd party smtp server. but the answer by Nip sounds like it is fairly usefull too

Nnp
  • 1,813
  • 7
  • 36
  • 62
3

I experienced the same issue when sending high volume email. Setting the deliveryMethod property to PickupDirectoryFromIis fixed it for me. Also don't create a new SmtpClient everytime.

Romhein
  • 2,126
  • 2
  • 17
  • 17
  • Thanks Nip, are you using local smtp server? in my case i have 3rd party smtp(with fixed ip), does this work fix work with 3rd party server? – Nnp Feb 09 '10 at 17:44
  • Well, with the `PickupDirectoryFromIis` set, the EML file is saved into the directory from which IIS picks up the e-mails to send. You still can test this by changing your local smtp configuration: IIS-> default SMTP server -> properties -> delivery -> advanced and then you can set up the smart host to be your 3d party IP address. – Romhein Feb 09 '10 at 18:20
1

For us, everything was fine, emails are very small and not a lot of them are sent and sudently it gave this error. It appeared that a technicien installed ASTARO which was preventing email to be sent. and we were getting this error so yes the error is a bit cryptic but I hope this could help others.

1

Seeing your loop for sending emails and the error which you provided there is only solution.
Declare the mail object out of the loop and assign fromaddress out of the loop which you are using for sending mails. The fromaddress field is getting assigned again and again in the loop that is your problem.

Widor
  • 13,003
  • 7
  • 42
  • 64
OM Krishna
  • 11
  • 1
1

what error do you get is it a SmtpFailedrecipientException? if so you can check the innerexceptions list and view the StatusCode to get more information. the link below has some good information

MSDN

Edit for the new information

Thisis a problem with finding your SMTP server from what I can see, though you say that it only happens on some emails. Are you using more than one smtp server and if so maybe you can tract the issue down to one in particular, if not it may be that the speed/amount of emails you are sending is causing your smtp server some issue.

Pharabus
  • 6,081
  • 1
  • 26
  • 39
  • Thanks Pharabus, i have modified my question with exception trace.it says "net_io_connectionclosed", what does that mean? – Nnp Feb 05 '10 at 19:39
  • Thanks Pharabus, i use only one smtp server for all. i found this http://forums.asp.net/t/924682.aspx , in the last post of this forum somebody says to use lient.ServicePoint.MaxIdleTime = 1; does that make sense? – Nnp Feb 05 '10 at 20:03
  • I would definately look at that, the other thing you could do is put a pause of a second or so in the loop depending on how you want to use the loop (obviously a 1 sec pause vs 60,000 loops adds quite a delay to this process) – Pharabus Feb 05 '10 at 20:18
  • i tried that, so far no error but that comes with the cost of cpu usage. it keep 20% cpu even there are no emails to send. and number of email will go over 100,000 soon, so i cant add pause. – Nnp Feb 05 '10 at 20:45
  • i tried client.ServicePoint.MaxIdleTime = 1 that does not work, then i tried client.ServicePoint.ConnectionLeaseTimeout = 0 even that does not work. i am stuck please help. – Nnp Feb 08 '10 at 22:03
  • sorry, just got to this. The problem definately points to your smtp server either not handling or maybe even throttling the amount of emails you can send in a batches. If the smtp server is run by a 3rd party you could maybe speak to them about it, if you own it lok to see if there is any throttling. the answer by Nip sounds like it is fairly usefull too – Pharabus Feb 09 '10 at 13:47
1

Five years later (I hope this developer isn't still waiting for a fix to this..)

I had the same issue, caused by the same error: I was declaring the SmtpClient inside the loop.

The fix is simple - declare it once, outside the loop...

MailAddress mail = null;
SmtpClient client = new SmtpClient();
client.Port = 25;
client.EnableSsl = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.Host = smtpAddress;       //  Enter your company's email server here!

for(int i = 0; i < number ; i++)
{
    mail = new MailMessage(iMail.from, iMail.to);
    mail.Subject = iMail.sub;
    mail.Body = iMail.body;
    mail.IsBodyHtml = true;
    mail.Priority = MailPriority.Normal;
    mail.Sender = from;
    client.Send(mail);
}
mail.Dispose();
client.Dispose();
Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
0

This error can appear when the web server can't access the mail server. Make sure the web server can reach the mail server, for instance pinging it.