我必须发送带有多个接收器的邮件。我使用mailmessage对象,其“to”属性包含分号分隔的地址。当我捕获异常时有没有办法知道女巫地址产生错误?
我知道我可以使用循环,但我认为这种方式对于大地址列表和附加文件更有效。
我使用下面的代码:
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);
}
}
}
答案 0 :(得分:1)
如果我从文档(http://msdn.microsoft.com/en-us/library/system.net.mail.smtpfailedrecipientsexception(v=vs.110).aspx)中正确阅读,您应该能够从Exception的FailedRecipient属性中读取失败的地址。要获得正确类型的异常,请更改catch块以处理SmtpFailedRecipientsException异常类型。
catch (SmtpFailedRecipientsException smtpSendException)
{
err.setErreur(smtpSendException.FailedRecipient...);
}