.NET SmtpClient:有没有一种方法,以确保前发送MailMessage所有的电子邮件解决?有的、电子邮件、方法、以确保

2023-09-05 00:42:18 作者:吹箫望江南

我使用SmtpClient发送电子邮件给多个收件人。当员工离开公司,他们的电子邮件地址无效。由于他们的电子邮件地址留在我们的数据库中,直到手动删除,试图发送一封电子邮件给他们造成在我们的应用程序引发异常SmtpClient.Send(MailMessage)。然而,尽管在例外的被抛出,它仍发送电子邮件。这是一个问题,因为我们希望通过阻止用户尝试保存记录并显示一个友好的信息,建议从数据库中删除任何无效的同事来处理这个错误。

I am using SmtpClient to send an email to multiple recipients. As employees leave the company, their email addresses become invalid. Since their email addresses remain in our database until deleted manually, trying to send an email to them causes our application to throw an exception during SmtpClient.Send(MailMessage). However, in spite of the exception being thrown, it still sends the email. This is a problem because we want to handle this error by blocking the user's attempt to save the record and display a friendly message advising to delete any invalid associates from the database.

如果有一种方法来遍历所有的recipeients以确保它们都是有效的,我们就可以将所有的电子邮件被发送到用户satisifes一组条件。

If there were a way to iterate through all the recipeients to make sure they're all valid, we can keep all emails from being sent until the user satisifes a set of conditions.

推荐答案

它是一种非常古老的问题,我不知道,如果你已完成了解决。

Its a very old question, I don't know if you have got it solved.

根据MSDN:的http:// MSDN .microsoft.com / EN-US /库/ swas0fwc(V = VS.100)的.aspx

在使用的发送的给多个收件人和SMTP发送电子邮件   服务器接受某些收件人是有效的,并拒绝别人,发送的发   电子邮件的收件人接受,然后    SmtpFailedRecipientsException 的被抛出。该异常会包含一个   这被拒绝收件人的上市。

When sending e-mail using Send to multiple recipients and the SMTP server accepts some recipients as valid and rejects others, Send sends e-mail to the accepted recipients and then a SmtpFailedRecipientsException is thrown. The exception will contain a listing of the recipients that were rejected.

这是从MSDN再追此异常所采取的一个例子:

This is an example of catching this exception taken from MSDN:

try {
    client.Send(message);
}
catch (SmtpFailedRecipientsException ex) {
    for (int i = 0; i < ex.InnerExceptions.Length; i++) {
        SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
        if (status == SmtpStatusCode.MailboxBusy || status == SmtpStatusCode.MailboxUnavailable) {
            Console.WriteLine("Delivery failed - retrying in 5 seconds.");
            System.Threading.Thread.Sleep(5000);
            client.Send(message);
        } 
        else {
            Console.WriteLine("Failed to deliver message to {0}", ex.InnerExceptions[i].FailedRecipient);
        }
    }
}

完整的例子在这里:http://msdn.microsoft.com/en-us/library/system.net.mail.smtpfailedrecipientsexception.aspx?cs-save-lang=1&cs-lang=csharp#$c$c-snippet-2

内部的发送使用状态code从 RCPT 返回命令,以提高相应的异常。

Internally the Send uses the statuscode returned from RCPT TO command to raise the appropriate exception.

检查 RecipientCommand.Send 法的实施 prepareCommand smtpTransport.SendMail (此方法内部调用 SmtpClient.Send )。它使用 RCPT TO 获得状态code ,然后在解析了 CheckResponse 方法和相应的 SmtpFailedRecipientsException 上升。然而,VRFY和RCPT两者都不是很可靠的,因为该邮件服务器倾向于延迟(油门NDR)或吞下响应作为防垃圾邮件措施。

Check the implementation for PrepareCommand in the RecipientCommand.Send method of smtpTransport.SendMail (This method is called internally by SmtpClient.Send). It uses RCPT TO to get the StatusCode which is then parsed in the CheckResponse method and accordingly the SmtpFailedRecipientsException is raised. However, VRFY and RCPT both are not very reliable because the mail servers tend to delay (throttle NDR) or swallow the response as an anti-spam measure.