I have to send mail with multiple receivers. I use a mailmessage object with a "to" property containing semi-colon separated addresses. When i catch exception is there a way to know witch address generate the error?

I know i can use a loop but i think that way is more efficient for big address list and attached files.

I use the code below :

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);
                }
            }
        }
有帮助吗?

解决方案

If I read correctly from documentation (http://msdn.microsoft.com/en-us/library/system.net.mail.smtpfailedrecipientsexception(v=vs.110).aspx) you should be able to read failed addresses from FailedRecipient property of Exception. To get proper type of exception, change your catch block to handle SmtpFailedRecipientsException kind of exceptions.

catch (SmtpFailedRecipientsException smtpSendException)
{
 err.setErreur(smtpSendException.FailedRecipient...);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top