有没有一种方法可以跟踪C#中gmail传递子系统拒绝的电子邮件

本文关键字:gmail 子系统 电子邮件 拒绝 跟踪 一种 方法 有没有 | 更新日期: 2023-09-27 18:25:51

我尝试使用不存在的电子邮件进行测试。

...
mail.To.Add(new MailAddress("nonexistenceemail@gmail.com"));
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
SmtpClient client= new SmtpClient();
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
client.SendAsync(mail, "Send Async");

回调定义如下:

private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
   e.Cancelled || e.Error != null ? doSomething("fail") : doSomething("success");
}

然后,当我检查邮箱时,有一封来自gmail递送子系统的被拒绝的电子邮件。

 Delivery to the following recipient failed permanently:
 nonexistenceemail@gmail.com
 Technical details of permanent failure:
 Google tried to deliver your message, but it was rejected by the server for ...
 The error that the other server returned was:
 550-5.1.1 The email account that you tried to reach does not exist. Please try
 550-5.1.1 double-checking the recipient's email address for typos or
 550-5.1.1 unnecessary spaces. Learn more at
 550 5.1.1 http://support.google.com/mail/bin/answer.py?answer=6596 cy1si14769573pdb.248 - gsmtp

然而,SendCompletedCallback告诉没有取消或错误,它可能不会被设计为以这种方式进行回调。在C#代码中,有没有一种方法可以识别这种被拒绝的电子邮件?

有没有一种方法可以跟踪C#中gmail传递子系统拒绝的电子邮件

当您的电子邮件被拒绝时,这种情况发生在邮件成功发送之后。例如,邮箱可能已满、邮件地址不存在等等。你必须获取绑定邮件并检查它们的标题,才能向你的系统提供失败的原因。不幸的是,大多数电子邮件提供商不会立即发回这些错误,因此没有万无一失的方法来做到这一点;活";,也不是所有的供应商都遵守RFC,特别是小型特殊公司的设置。

也许你可以从RFC中获得解决方案的灵感

https://www.rfc-editor.org/rfc/rfc5429