【发布时间】: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