smtpclient " failure sending mail"

111,486

Solution 1

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

Solution 2

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.

Solution 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.

Solution 4

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.

Solution 5

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.

Share:
111,486
Nnp
Author by

Nnp

enthusiastic developer.

Updated on February 05, 2021

Comments

  • Nnp
    Nnp over 3 years

    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)

  • Nnp
    Nnp over 14 years
    Thanks Mitchel, i use same "from" email address for all, but most of them got delivered. that means option (2) left out for check?
  • Mitchel Sellers
    Mitchel Sellers over 14 years
    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.
  • Nnp
    Nnp over 14 years
    Thanks Pharabus, i have modified my question with exception trace.it says "net_io_connectionclosed", what does that mean?
  • Nnp
    Nnp over 14 years
    Thanks Mitchel, i have modified my question with exception trace.it says "net_io_connectionclosed", what does that mean?
  • Mitchel Sellers
    Mitchel Sellers over 14 years
    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
  • Nnp
    Nnp over 14 years
    Thanks Pharabus, i use only one smtp server for all. i found this 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
    Nnp over 14 years
    Thanks Mitchel, i dont have access to SMTP logs :(. i found this 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?
  • Pharabus
    Pharabus over 14 years
    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)
  • Nnp
    Nnp over 14 years
    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
    Nnp over 14 years
    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
    Nnp over 14 years
    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
    Nnp over 14 years
    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.
  • Pharabus
    Pharabus over 14 years
    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
  • Nnp
    Nnp over 14 years
    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?
  • Romhein
    Romhein over 14 years
    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.