2014-10-08 42 views
0

我试图使用一个不存在的电子邮件来测试。有没有办法在C#中跟踪来自gmail传递子系统的被拒绝的电子邮件?

... 
mail.To.Add(new MailAddress("[email protected]")); 
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: 

[email protected] 

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#代码中,有没有一种方法可以识别被拒绝的电子邮件?

回答

0

当您的电子邮件被拒绝时,会在邮件成功发送后发生。例如,邮箱可能已满,不具有远程邮件地址等。 您必须获取反弹邮件并检查其标题,以便能够为您的系统提供失败的原因。 不幸的是,大多数电子邮件提供商不会立即发送这些错误,所以没有万无一失的方法来做到这一点“生活”,也不是所有提供商都遵守RFC,特别是小型特殊公司设置。

也许你能得到的灵感为你解掉RFC对这些案件

http://tools.ietf.org/html/rfc5429

相关问题