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:
- 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.
- 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)