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

查看:13
本文介绍了在 .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}
	Smtp: {1}{2}:{3}
	Status Code: {4}
	Faild 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}
	Smtp: {1} - {2}:{3}
	Status 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}.
	Source: {1}
	Stack Trace: {2}", e.Message, e.Source, e.StackTrace));

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

但是这种方法不允许我捕获一些更高级"的 SMTP 错误,例如 No Domain、No such Email 等.

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天全站免登陆