.Net smtp 使用 SmtpStatusCode 发送 - 何时应重试?
我有一项发送电子邮件并将出站电子邮件存储在数据库中的服务。 我正在使用 .NET 本机 Smtp 类进行电子邮件传输。如果电子邮件发送失败,我会设置一个错误标志。
我的服务将定期检查未送达的消息并尝试重新发送。什么情况下应该重试发送邮件?我注意到,即使电子邮件地址不正确,它也会引发异常,但我希望我的服务放弃任何无效电子邮件,否则它将永远重试。
基本上,我想捕获异常,即很有可能重新发送电子邮件。我想这只是网络错误而不是电子邮件帐户。 哪个 SmtpStatusCode 表明值得重试:
http:// /msdn.microsoft.com/en-us/library/system.net.mail.smtpstatuscode.aspx
I have a service which sends email and stores the outbound email in a database.
I'm using .NET native Smtp class for email transmission. I have an error flag which is set if the email failed delivery.
My service will periodically check for undelivered messages and try to re-send. Under what circumstances should it retry to send the email? I've noted that even if the email address is incorrect it will throw an exception, but I want my service to ditch any invalid emails otherwise it will retry forever.
Basically, I want to catch the exception whereby there is a good chance the email can be re-delivered. I guess this would be only network errors rather than email accounts.
Which of the SmtpStatusCode would indicate worthy of retry:
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpstatuscode.aspx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在尝试解决与您完全相同的问题。我想出的解决方案涉及大约一个小时的时间来研究 RFC2821 并尽我所能地解释它。如果您有兴趣,可以在这里找到它 - http://www.rfc-editor.org /rfc/rfc2821.txt
阅读 RFC 的最大收获是
4yz
形式的代码表示暂时的负完成代码,需要重试。5yz
形式的代码表示永久否定完成代码,您不应重试。因此,目标是过滤掉需要重试和不需要重试的收件人。所有
5yz
代码都可以解释为“不要重试”,但 552 除外,某些服务器可能会错误地发送 552,而不是 452。从那时起,您需要决定:BadCommandSequence
),这会导致您的服务快速失败,而无需重试任何操作。或者
因此,我的方法是这样的:
SmtpFailedRecipientsException
和SmtpFailedRecipientException
。HandleFailedRecipient()
方法来解释 SMTP 代码。如果该方法在任何时候返回false
,那么这表明存在更严重的错误,我的服务应该会快速失败(无需尝试重新发送电子邮件)。这是我解释 SMTP 代码的方法:
让我知道这是否有意义或者您是否需要查看更多代码,但从这里应该相对清晰。
I'm trying to solve the exact same problem you are. The solution I came up involved about an hour of pouring through RFC2821 and interpreting it as best as I could. If you are interested you can find it here - http://www.rfc-editor.org/rfc/rfc2821.txt
The big take-away from reading the RFC is that codes of the form
4yz
indicate a transient negative completion code, which warrants a retry. Codes of the form5yz
indicate a permanent negative completion code, which you should NOT retry.So the goal is to filter out recipients where a retry is warranted and where it isn't. All of the
5yz
codes can be interpreted as "don't retry" with the exception of 552, which some servers may erroneously send instead of 452. From there on out you need to decide:BadCommandSequence
) which should cause your service to fail fast without retrying anything.OR
Thus, my approach is such:
SmtpFailedRecipientsException
andSmtpFailedRecipientException
.HandleFailedRecipient()
method which interprets the SMTP code. If at any time that method returnsfalse
then this indicates a much more serious error and my service should just fail fast (without trying to resend the email).Here is my method which interprets the SMTP code:
Let me know if this makes sense or if you need to see more code, but it should be relatively clear-cut from here.