在.NET中捕获SMTP错误 [英] Capture SMTP errors in .NET

查看:275
本文介绍了在.NET中捕获SMTP错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常情况下,我会发送电子邮件这样的:

Normally, I'd send emails like that:

int statusCode = 0;
string error = null;

try
{
    smtp.Send(mailMessage);
    statusCode = 250;
}
catch (SmtpFailedRecipientException e)
{
    Log(String.Format("[Exception] {0}\n\tSmtp: {1}{2}:{3}\n\tStatus Code: {4}\n\tFaild Recipient: {5}", e.Message, smtp.Key, smtp.Value.Host, smtp.Value.Port, e.StatusCode, e.FailedRecipient));

    statusCode = (int)e.StatusCode;
    error = e.Message;
}
catch (SmtpException e)
{
    Log(String.Format("[Exception] {0}\n\tSmtp: {1} - {2}:{3}\n\tStatus Code: {4}", e.Message, smtp.Key, smtp.Value.Host, smtp.Value.Port, e.StatusCode));

    statusCode = (int)e.StatusCode;
    error = e.Message;
}
catch (Exception e)
{
    Log(String.Format("[Exception] {0}.\n\tSource: {1}\n\tStack Trace: {2}", e.Message, e.Source, e.StackTrace));

    statusCode = -1;
    error = "General Failure";
}

但这种方法并不让我赶上了一些更高级SMTP错误,如无域,没有这样的电子邮件,等等。

But this method doesn't allow me to catch some of the more "advanced" SMTP errors such as No Domain, No such Email, etc.

我如何能捕捉那种SMTP错误?是,即使可能吗?

How can I capture that kind of SMTP errors? Is that even possible?

例如,当我试图在Gmail中发送到一个地址,如 asdfkljhadf@dgdfasdf.com ,Gmail中发送了一段时间后,一封电子邮件, asdfkljhadf@dgdfasdf.com 不存在。

For example, when I'm trying to send in Gmail to an address such as asdfkljhadf@dgdfasdf.com, Gmail sends me after a while an Email that asdfkljhadf@dgdfasdf.com doesn't exist.

感谢你。

推荐答案

SMTP是一个存储转发协议,这意味着服务器可以接受转发的消息,但不知道在当时,该消息无法交付。因此,举例来说,当你告诉你的ISP的SMTP服务器将邮件传递到invalidaddress@example.com,服务器的响应是,好吧,我会转发。在一段时间以后,ISP的SMTP服务器将联系的SMTP服务器example.com,并尝试发送邮件。只有这样,它知道邮件无法送达。

SMTP is a "store and forward" protocol, meaning that the server can accept a message for forwarding, but not know at the time that the message can't be delivered. So, for example, when you tell your ISP's SMTP server to deliver a message to "invalidaddress@example.com", the server's response is, "Okay, I'll forward it." At some later time, the ISP's SMTP server will contact the SMTP server for "example.com", and try to deliver the message. Only then does it know that the mail is undeliverable.

所以,只要你的客户端和SMTP服务器之间的通信而言,在发送邮件成功 - 服务器同意转发。因此,也不例外。

So as far as the communication between your client and the SMTP server is concerned, the message was delivered successfully--the server agreed to forward it. Therefore, no exception.

这篇关于在.NET中捕获SMTP错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆