【问题标题】:error analyse with multi receiver mailmessage使用多接收方邮件消息进行错误分析
【发布时间】:2013-12-26 14:41:50
【问题描述】:

我必须使用多个收件人发送邮件。我使用带有“to”属性的 mailmessage 对象,该属性包含分号分隔的地址。当我捕获异常时,有没有办法知道女巫地址产生错误?

我知道我可以使用循环,但我认为这种方式对于大地址列表和附加文件更有效。

我使用下面的代码:

MailMessage message = new MailMessage();
        message.From = new MailAddress(txtDe.Text);
        message.Bcc.Add(mails);
        message.Body = CKEditor1.Text;
        message.IsBodyHtml = true;
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.Subject = txtObj.Text;
        message.SubjectEncoding = System.Text.Encoding.UTF8;

        string txt=Regex.Replace(CKEditor1.Text, @"<[^>]*>", String.Empty);
        var plainView = AlternateView.CreateAlternateViewFromString(txt, null, "text/plain");

        var htmlView = AlternateView.CreateAlternateViewFromString(CKEditor1.Text, null, "text/html");
        message.AlternateViews.Add(plainView);
        message.AlternateViews.Add(htmlView);

        string[] foldersIds = Session["uploadedFoldersId"].ToString().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

        foreach (string folderId in foldersIds)
        {
            string tempPath = Path.Combine(new string[] { Path.GetTempPath(), "_AjaxFileUpload", folderId });

            DirectoryInfo di = new DirectoryInfo(tempPath);
            FileInfo[] files = di.GetFiles();

            smtp.attachement[] att = new smtp.attachement[files.Length];

            foreach (FileInfo f in files)
            {
                message.Attachments.Add(new Attachment(f.FullName));
            }
        }
        try
        {
            client.Send(message);
            message.Dispose();

            Response.Redirect(tools.urlRetour(Request.QueryString["retour"]));
        }
        catch (Exception ex)
        {
            err.setErreur("Erreur à l'envoi!", ex.ToString(), 0);
            if (last) displayInfo();
        }

        finally
        {
            if (last)
            {
                foreach (string folderId in foldersIds)//remove temporary uploaded files folders
                {
                    string tempPath = Path.Combine(new string[] { Path.GetTempPath(), "_AjaxFileUpload", folderId });

                    DirectoryInfo di = new DirectoryInfo(tempPath);
                    di.Delete(true);
                }
            }
        }

【问题讨论】:

    标签: c# mailmessage


    【解决方案1】:

    如果我从文档 (http://msdn.microsoft.com/en-us/library/system.net.mail.smtpfailedrecipientsexception(v=vs.110).aspx) 中正确阅读,您应该能够从异常的 FailedRecipient 属性中读取失败的地址。要获得正确类型的异常,请更改您的 catch 块以处理 SmtpFailedRecipientsException 类型的异常。

    catch (SmtpFailedRecipientsException smtpSendException)
    {
     err.setErreur(smtpSendException.FailedRecipient...);
    }
    

    【讨论】:

    • 没错,但你必须使用没有 s 的 SmtpFailedRecipientException 并且它可以工作,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 2012-09-19
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多