views:

1398

answers:

4

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)

+1  A: 

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
Thanks Pharabus, i have modified my question with exception trace.it says "net_io_connectionclosed", what does that mean?
Nnp
Thanks Pharabus, i use only one smtp server for all. i found thishttp://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
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
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
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
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
A: 

Well, the "failure sending e-mail" should hopefully have a bit more detail. But thee 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.

Mitchel Sellers
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
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
Thanks Mitchel, i have modified my question with exception trace.it says "net_io_connectionclosed", what does that mean?
Nnp
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
Thanks Mitchel, i dont have access to SMTP logs :(. i found thishttp://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
That could help
Mitchel Sellers
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
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
A: 

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.

Nip
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
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.
Nip
A: 

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