使用ASP.NET发送电子邮件的临时问题

时间:2021-03-20 16:37:05

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

我讨厌在这里发布堆栈跟踪,但我没有想法......我的应用程序在网站上发送各种任务的电子邮件,90%的时间没有问题。但是,ASP.NET经常会抛出一个奇怪的异常,这似乎与邮件服务器连接有关(我使用谷歌业务服务器来处理电子邮件)。这个错误对我来说太通用了,所以下面的堆栈跟踪。我的应用程序捕获异常并回写一个通用的“在5分钟响应中再试一次”。

I'm stumped by this error because:

我被这个错误困扰了,因为:

  • Google email is generally very reliable (it is being sent from my domain, not gmail.com).
  • 谷歌电子邮件通常非常可靠(它是从我的域发送的,而不是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.
  • 在5分钟内再次尝试几乎总是允许发送电子邮件没有问题,没有任何参数(收件人,主题,正文)发生了变化。

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. 电子邮件发送是同步的这就是我实际想要发生的事情,因为我想用成功/失败状态消息来响应用户。

  3. I am using Amazon EC2 as a server, if that has any bearing on the matter.
  4. 我正在使用Amazon EC2作为服务器,如果这与此事有任何关系。

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)

2 个解决方案

#1


5  

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:

我过去曾经在批量电子邮件系统上工作过。您获得的异常消息完全是描述性的:SMTP服务器暂时不接受邮件。这可能是由于许多条件:

  1. The server is busy
  2. 服务器很忙

  3. You are sending too many emails too fast (this can make you look like a spammer)
  4. 您发送太多电子邮件太快(这可能会让您看起来像垃圾邮件发送者)

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

以后重试电子邮件始终有效的事实表明这是正常的预期行为。没有什么可以做的不同以避免这种情况,这是ISP在需要时用来限制流量的常用方法。

#2


0  

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.

除了Dave Swersky已经说过的内容之外:你可以存储你想要发送到某个地方的电子邮件(比如文件系统或数据库),并让电子邮件生成一个服务。您的服务会关注要发送的新电子邮件。如果发送时发生错误,您可以将电子邮件放回队列中,稍后再试。

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.

也许您甚至可以使用使用GoogleMail作为中继服务器的本地STMPServer。在这种情况下,您可以使用内置的拾取目录功能,而不是自己编写。

#1


5  

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:

我过去曾经在批量电子邮件系统上工作过。您获得的异常消息完全是描述性的:SMTP服务器暂时不接受邮件。这可能是由于许多条件:

  1. The server is busy
  2. 服务器很忙

  3. You are sending too many emails too fast (this can make you look like a spammer)
  4. 您发送太多电子邮件太快(这可能会让您看起来像垃圾邮件发送者)

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

以后重试电子邮件始终有效的事实表明这是正常的预期行为。没有什么可以做的不同以避免这种情况,这是ISP在需要时用来限制流量的常用方法。

#2


0  

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.

除了Dave Swersky已经说过的内容之外:你可以存储你想要发送到某个地方的电子邮件(比如文件系统或数据库),并让电子邮件生成一个服务。您的服务会关注要发送的新电子邮件。如果发送时发生错误,您可以将电子邮件放回队列中,稍后再试。

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.

也许您甚至可以使用使用GoogleMail作为中继服务器的本地STMPServer。在这种情况下,您可以使用内置的拾取目录功能,而不是自己编写。