tags:

views:

105

answers:

2

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)
+3  A: 

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.

Dave Swersky
As based on your experiences... Is it a good idea to try a second time immediately after the first SMTP failure occurs, with a 1 second delay between? This does seem to be temporary issue only (surprising, cause it is google mail!!?) and simply repeating the send seems to overcome problems.
Simon Ellis
I guarantee you're not the only one getting this error sending through Google, or any other ISP/ESP for that matter. This is not necessarily a "failure" on Google's end- the queue on that server could be full or any of a number of other common issues. One second is probably not enough, I would wait at least 30 seconds.
Dave Swersky
Thanks for the advice!
Simon Ellis
A: 

In addition to what Dave Swersky already said: You could store the emails you want to send somewhere (like filesystem or database) and let the e-mail generation be handeld by a service. Your service would watch for new emails to send. If an error occurs while sending, you could put the e-mail back into the queue and try again later.

Maybe you can even use a local STMPServer that uses GoogleMail as relay server. It that case you could use the built-in pickupdirectory functionality instead of writing it yourself.

citronas