捕获 .NET 中的 SMTP 错误

发布于 2024-10-18 08:00:48 字数 1356 浏览 6 评论 0原文

通常,我会像这样发送电子邮件:

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 错误,例如“无域”、“没有这样的电子邮件”等。

我如何捕获此类 SMTP 错误?这可能吗?

例如,当我尝试通过 Gmail 发送到诸如 [ email protected],Gmail 稍后会向我发送一封电子邮件,其中 [电子邮件受保护] 不存在。

谢谢。

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

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

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

For example, when I'm trying to send in Gmail to an address such as [email protected], Gmail sends me after a while an Email that [email protected] doesn't exist.

Thank you.

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

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

发布评论

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

评论(2

云归处 2024-10-25 08:00:48

SMTP 是一种“存储并转发”协议,这意味着服务器可以接受邮件进行转发,但当时并不知道该邮件无法传送。因此,例如,当您告诉 ISP 的 SMTP 服务器将邮件传送到“[email] 时, protected]”,服务器的响应是“好的,我会转发它。”稍后,ISP 的 SMTP 服务器将联系“example.com”的 SMTP 服务器,并尝试传送邮件。只有这样它才知道邮件无法送达。

因此,就您的客户端和 SMTP 服务器之间的通信而言,消息已成功传递 - 服务器同意转发它。因此,也不例外。

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 "[email protected]", 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.

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.

紅太極 2024-10-25 08:00:48

我认为 smtp 服务器在发送电子邮件时并不知道这些错误。 Smtp 服务器在尝试查找域/坏电子邮件等后收到其他服务器的响应时会发现这些错误。

如果您使用 IIS,您可能会在 IIS 的坏电子邮件文件夹中找到此类电子邮件。

I don't think those errors are known by the smtp server at the time of sending email. Smtp server find out about those errors when it gets a response from other servers after trying to find the domain / bad email etc.

If you are using IIS, you may find such emails in bad email folder in IIS.

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