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

发布于 2024-09-18 00:54:56 字数 1437 浏览 5 评论 0原文

我不想在这里发布堆栈跟踪,但我没有主意......我的应用程序为网站上的各种任务发送电子邮件,90% 的时间都没有问题。然而,ASP.NET 经常会抛出一个奇怪的异常,这似乎与邮件服务器连接有关(我使用 google 业务服务器来发送电子邮件)。该错误对于我来说太通用了,无法调试,因此出现了下面的堆栈跟踪。我的应用程序捕获异常并写回通用的“5 分钟内重试响应”。

我被这个错误难住了,因为:

  • Google 电子邮件通常非常可靠(它是从我的域发送的,而不是 gmail.com)。
  • 数量非常低,因此这不应该是加载/垃圾邮件类型的问题。
  • 错误非常普遍。
  • 5 分钟内重试几乎总是可以毫无问题地发送电子邮件,并且任何参数(收件人、主题、正文)都没有更改。

有什么想法吗?我能想到的唯一突出问题是:

  1. 电子邮件发送是同步的。这就是我真正想要发生的事情,因为我想用成功/失败状态消息响应用户。
  2. 我正在使用 Amazon EC2 作为服务器,如果这对此事有任何影响的话。

代码:

    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);
    }

异常:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泼猴你往哪里跑 2024-09-25 00:54:56

我过去曾在批量电子邮件系统上工作过。您收到的异常消息完全是描述性的:SMTP 服务器暂时不接受邮件。这可能是由多种原因造成的:

  1. 服务器繁忙
  2. 您发送的电子邮件太多且速度太快(这可能会让您看起来像垃圾邮件发送者)
  3. 一般错误

稍后重试电子邮件始终有效的事实表明这是正常的,符合预期行为。您无法采取其他措施来避免这种情况,这是 ISP 在需要时用来限制流量的常用方法。

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.

花桑 2024-09-25 00:54:56

除了戴夫·斯沃斯基已经说过的话:
您可以将要发送的电子邮件存储在某个地方(例如文件系统或数据库),并让服务生成电子邮件。您的服务将监视要发送的新电子邮件。如果发送时发生错误,您可以将电子邮件放回队列中,稍后再试。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文