C# 发送电子邮件的任务中的密件抄送电子邮件地址异常处理

C# 发送电子邮件的任务中的密件抄送电子邮件地址异常处理,c#,task-parallel-library,C#,Task Parallel Library,我设置了一个异步发送电子邮件的任务,并尝试处理发送电子邮件失败的电子邮件地址的异常。它成功注销了异常,但我想知道是否能够从Bcc列表中注销导致异常发生的电子邮件地址。下面是我的代码,任何关于这方面的建议都会很有帮助 public void SendEmail(string from, string copyToSender, List<string> bccRecipient, string subject, string body, SmtpSection smtpSection)

我设置了一个异步发送电子邮件的任务,并尝试处理发送电子邮件失败的电子邮件地址的异常。它成功注销了异常,但我想知道是否能够从Bcc列表中注销导致异常发生的电子邮件地址。下面是我的代码,任何关于这方面的建议都会很有帮助

public void SendEmail(string from, string copyToSender, List<string> bccRecipient, string subject, string body, SmtpSection smtpSection)
{
    var context = HttpContext.Current;

    if (smtpSection != null)
    {
        Task.Factory.StartNew(() =>
        {
            var mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(from, smtpSection.Network.TargetName);
            mailMessage.Subject = subject;
            mailMessage.Body = body;
            mailMessage.IsBodyHtml = true;

            myMailMessage = mailMessage;

            //send emails to which to Bcc'd including the From Person Email
            myMailMessage.Bcc.Add(copyToSender);
            foreach (var r in bccRecipient)
            {
                myMailMessage.Bcc.Add(r);
            }
            //incorrect email address added to log out the exception against it 
            myMailMessage.Bcc.Add("foo");

            using (var client = new SmtpClient())
            {
                client.Host = smtpSection.Network.Host;
                client.EnableSsl = smtpSection.Network.EnableSsl;
                client.Port = Convert.ToInt32(smtpSection.Network.Port);
                client.Credentials = new NetworkCredential(smtpSection.Network.UserName,
                    smtpSection.Network.Password);
                client.Send(mailMessage);
            }

        }).ContinueWith(tsk =>
        {
            var recipientEmail=//how to figure this out??
            //something broke
            var flattened = tsk.Exception.Flatten();

            flattened.Handle(ex =>
            {
                _mailLogger.LogError
                  (recipientEmail, context.Request.UrlReferrer.OriginalString, ex.Message);
                return true;
            });
        }, TaskContinuationOptions.OnlyOnFaulted); ;
    }
}
public void sendmail(string from、string copyToSender、List bc客户、string subject、string body、SmtpSection SmtpSection)
{
var context=HttpContext.Current;
if(smtpSection!=null)
{
Task.Factory.StartNew(()=>
{
var mailMessage=new mailMessage();
mailMessage.From=新邮件地址(From,smtpSection.Network.TargetName);
mailMessage.Subject=主题;
mailMessage.Body=Body;
mailMessage.IsBodyHtml=true;
myMailMessage=mailMessage;
//向密件抄送人发送电子邮件,包括发件人电子邮件
myMailMessage.Bcc.Add(copyToSender);
foreach(b客户中的var)
{
myMailMessage.Bcc.Add(r);
}
//添加了不正确的电子邮件地址以针对其注销异常
myMailMessage.Bcc.Add(“foo”);
使用(var client=new SmtpClient())
{
client.Host=smtpSection.Network.Host;
client.enablesl=smtpSection.Network.enablesl;
client.Port=Convert.ToInt32(smtpSection.Network.Port);
client.Credentials=新的网络凭据(smtpSection.Network.UserName,
smtpSection.Network.Password);
client.Send(mailMessage);
}
}).ContinueWith(tsk=>
{
var recipientEmail=//如何解决这个问题??
//有东西坏了
var Flatten=tsk.Exception.Flatten();
展平.句柄(ex=>
{
_mailLogger.LogError
(recipientEmail,context.Request.urlReferer.OriginalString,例如Message);
返回true;
});
},TaskContinuationOptions.OnlyOnFaulted);
}
}

如果您的邮件在范围内,我想您可以找到密件抄送。你只需要声明,这样就可以在你的ContinueWith中看到它。我会让邮件消息在范围内。但是我怎么才能得到失败的电子邮件呢?如果你是说邮件在无法传递的意义上失败了,你必须通过编程来检查邮局主管的失败响应。我以前做过这个。这不是很容易,但可以做到。我在例外中找不到任何线索。你能提供多一点信息吗?我如何检查邮局主管的失败响应?什么是“不正确”的电子邮件地址?如果您的意思是地址格式错误,只需
尝试添加
捕获(FormatException)
我想如果您的邮件在范围内,您可以找到密件抄送。你只需要声明,这样就可以在你的ContinueWith中看到它。我会让邮件消息在范围内。但是我怎么才能得到失败的电子邮件呢?如果你是说邮件在无法传递的意义上失败了,你必须通过编程来检查邮局主管的失败响应。我以前做过这个。这不是很容易,但可以做到。我在例外中找不到任何线索。你能提供多一点信息吗?我如何检查邮局主管的失败响应?什么是“不正确”的电子邮件地址?如果您是指格式不正确的地址,只需
尝试添加
捕获(FormatException)