4

我想向多个地址(超过 1000 个用户)发送电子邮件并使用以下代码,当我使用它向少于 100 个用户发送电子邮件时它可以工作,但对于超过 100 个用户它不起作用并抛出 smtpfailedrecipientsexception 失败的收件人!为什么?如何向有效地址发送电子邮件并解决此错误?

public void SendMailMessage (string[] to,string message,string subject) 
    {
        MailMessage mMailMessage = new MailMessage ();
        int lenght = to.GetLength(0);
        if (lenght > 1) {
            foreach (string email in to) {
                mMailMessage.Bcc.Add ( email );
            }
        }
        else {
            mMailMessage.To.Add ( to[0] );
        }

                mMailMessage.From = new MailAddress ("no-replay@mycompany.net");                  
                SmtpClient mSmtpClient = new SmtpClient ();
                mMailMessage.Body = message;
                mMailMessage.IsBodyHtml = true;
                mMailMessage.Priority = MailPriority.Normal;
                mMailMessage.Subject = subject;
                mSmtpClient.EnableSsl = true;                
                ServicePointManager.ServerCertificateValidationCallback = delegate(object s,X509Certificate certificate,X509Chain chain, SslPolicyErrors sslPolicyErrors) {return true;};                    
            try {  
                mSmtpClient.Send (mMailMessage);                
        }
        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)
                {
                    Logger.Debug("Delivery failed - retrying in 5 seconds.");
                    System.Threading.Thread.Sleep(5000);
                    //client.Send(message);
                    mSmtpClient.Send (mMailMessage);

                }
                else
                {
                    Logger.Debug (string.Format ("Failed to deliver message to {0},{1}", ex.InnerExceptions[i].FailedRecipient, i));

                }

            }
        }
        catch (Exception ex)
        {
            Logger.Debug (string.Format("Exception caught in RetryIfBusy(): {0}", ex.ToString() ));
        }

    }
4

2 回答 2

0

看起来您正在使用的 SMTP 服务器拒绝了超过 100 个收件人的邮件。

你可以:

  • 使用分发列表而不是直接向每个收件人发送消息
  • 发送不超过 100 个收件人的多条消息
  • 与此服务器的管理员交谈以更改限制

但请注意,当您向数百个外部收件人发送邮件时,垃圾邮件过滤器可能会将它们标记为垃圾邮件。

于 2013-05-04T10:20:20.493 回答
0

我在异常处理的 Microsoft 文档之一中发现了这一点,如下所示:

 var client = new System.Net.Mail.SmtpClient();
        try
        {
            client.Send(mailMessage);
        }
        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(mailMessage);
                }
                else
                {
                    Console.WriteLine("Failed to deliver message to {0}",
                        ex.InnerExceptions[i].FailedRecipient);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception caught in RetryIfBusy(): {0}",
                    ex.ToString());
        }

但我不知道如何为此异常 Microsoft 链接生成异常场景 - 有关模式详细信息

于 2020-04-14T06:42:51.607 回答