Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporary problems sending emails with ASP.NET

Tags:

c#

asp.net

smtp

I hate to just post a stack trace here, but I am out of ideas... My app sends emails for various tasks on the website, and 90% of the time there are no problems. However, every so often ASP.NET throws a weird exception, which seems to relate to mail server connectivity (I use google business servers for email). The error is too generic for me to debug, hence the stack trace below. My app captures the exception and writes back a generic 'try again in 5 minutes response'.

I'm stumped by this error because:

  • Google email is generally very reliable (it is being sent from my domain, not gmail.com).
  • Volumes are very low, so this shouldn't be a loading/spamming type of problem.
  • Error is very generic.
  • Trying again in 5 minutes almost always allows the email to be sent with no problem, with none of the parameters (recipient, subject, body) having changed.

Any ideas? The only outstanding issues I can think of is that:

  1. Email sending is synchronous. This is what I actually want to occur, as I want to respond to the user with a success/failure status message.
  2. I am using Amazon EC2 as a server, if that has any bearing on the matter.

The code:

    public static void SendMail(String from, String to, String subject, String body, bool IsHtml)
    {
        MailMessage m = new MailMessage(from, to, subject, body);
        m.IsBodyHtml = IsHtml;

        SmtpClient smtpClient = new SmtpClient();
        smtpClient.EnableSsl = true;
        smtpClient.Send(m);
    }

The exception:

System.Net.Mail.SmtpException: Error in processing. The server response was: 4.3.0 Mail server temporarily rejected message. m6sm2190005vcx.24
at System.Net.Mail.DataStopCommand.CheckResponse(SmtpStatusCode statusCode, String serverResponse)
at System.Net.Mail.DataStopCommand.Send(SmtpConnection conn)
at System.Net.Mail.SmtpConnection.OnClose(Object sender, EventArgs args)
at System.Net.ClosableStream.Close()
at System.Net.Mail.MailWriter.Close()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
like image 652
Simon at LabSlice-com Avatar asked Sep 07 '10 15:09

Simon at LabSlice-com


1 Answers

I've worked on bulk email systems in the past. The exception message you are getting is totally descriptive: that SMTP server is temporarily not accepting mail. This can be due to a number of conditions:

  1. The server is busy
  2. You are sending too many emails too fast (this can make you look like a spammer)
  3. General errors

The fact that retrying the email later always works indicates that this is normal, expected behavior. There is nothing you can do differently to avoid this, it's a common method used by ISPs to throttle traffic when needed.

like image 62
Dave Swersky Avatar answered Nov 04 '22 07:11

Dave Swersky