【问题标题】:Email failure when one email is wrong in same domain in asp.net当 asp.net 中同一域中的一封电子邮件错误时,电子邮件失败
【发布时间】:2018-12-04 22:31:21
【问题描述】:

我的项目中有一个“电子邮件”功能。如果同一域中的所有电子邮件ID都正确,电子邮件将成功发送。如果其中一封电子邮件有误,则电子邮件将失败并出现以下错误。

Exception:-The server rejected one or more recipient addresses. The server response was: 550 5.0.0 Requested action not taken: mailbox unavailable or not local.  , InnerException:-System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: The server rejected one or more recipient addresses. The server response was: 550 5.0.0 Requested action not taken: mailbox unavailable or not local.       --- End of inner exception stack trace ---     at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)     at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)     at System.Web.Mail.SmtpMail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args) , Stack Trace :-   at System.Web.Mail.SmtpMail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)     at System.Web.Mail.SmtpMail.CdoSysHelper.Send(MailMessage message)     at System.Web.Mail.SmtpMail.Send(MailMessage message)     at Entity.Common.Mail.SendMail(String mailFrom, String mailTo, String mailCc, String Subject, String Content, String Bcc, String optionalfrom)

代码如下

MailAddress xfrom = new MailAddress(EmailFrom);
        MailMessage mm = new MailMessage();
        mm.Sender = xfrom;
        mm.From = xfrom;

        //to
        string[] tos = EmailTO.Trim().Split(';');
        for (int i = 0; i < tos.Length; i++)
        {
            mm.To.Add(new MailAddress(tos[i]));
        }
        SmtpClient smtp = new SmtpClient();
        smtp.Host = smtpServer;
        smtp.Credentials = new NetworkCredential(smtpMailAccount, smtpMailPassword);
        smtp.Send(mm);
        IsSuccess = true;

如何忽略错误的电子邮件 ID 并发送电子邮件以保留所有正确的电子邮件 ID?即使存在错误的电子邮件 ID,我也想发送电子邮件。如何解决这个问题?帮助/建议表示赞赏。

【问题讨论】:

    标签: c# asp.net email smtpclient email-address


    【解决方案1】:

    将异常捕获为SmtpFailedRecipientException,它具有属性FailedRecipient。从To 集合中删除这个,然后重试直到不再抛出SmtpFailedRecipientException

    我在其中做的一个项目的一小段摘录,请注意,该异常还有一个复数版本,它的InnerExceptions 列表中可以包含多个失败的收件人:

    try
    {
        /* code to send */
    }
    catch (SmtpFailedRecipientsException recsex)
    {
        var failedRecipient = String.Empty;
        foreach (var rex in recsex.InnerExceptions)
        {
            /* rex.FailedRecipient contains the invalid address to remove
             * before the next attempt.
             * note that you might end up with an empty recipient list
             * in case all were invalid, then you should also no longer
             * attemtp to resubmit the mail */
        }
    }
    catch (SmtpFailedRecipientException recex)
    {
        /* recex.FailedRecipient contains the invalid address to remove
         * before the next attempt */
    }
    

    【讨论】:

      【解决方案2】:

      我不知道电子邮件id是什么。

      但如果你的目标是忽略一些电子邮件,你可以尝试一下。

      try {
          //yourcode
      } catch (Exception e)
      {
          if (/*Check if e is not equal to "mailbox unavailable or not local"*/) throw; //beacuse its an other error
          //your wrong email "id" error gets ignored
      
      }
      

      编辑:

      您可以在发送电子邮件之前检查收件人是否存在。看看STMP VRFY 命令。

      【讨论】:

      • 他们似乎每封邮件都有一个包含多个收件人的列表,并且希望该邮件发送给有效的收件人,即使其中一个或多个无效。当然,这可能是另一种方法,即按照您的建议忽略例外情况,每个收件人发送一封邮件,但这有两个后果:1)其他收件人不会在邮件上看到他们的共同收件人,以及 2)更多的 smtp 流量。跨度>
      猜你喜欢
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多