在.NET中捕获SMTP错误错误、NET、SMTP

由网友(心如荒岛)分享简介:通常情况下,我会发送电子邮件这样的:Normally, I'd send emails like that:int statusCode = 0;string error = null;try{smtp.Send(mailMessage);statusCode = 250;}catch (SmtpFaile...

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

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}ntSmtp: {1}{2}:{3}ntStatus Code: {4}ntFaild 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}ntSmtp: {1} - {2}:{3}ntStatus 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}.ntSource: {1}ntStack 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.

阅读全文

相关推荐

最新文章