1

I setup a task to send an Email asynchronously and trying to handle exception against an Email Address that has been failed for sending an email. It successfully log out the exception but I wonder if I am able to log out the Email Address from Bcc list for which causes the exception to occur. Below is my code, any suggestion on this would be helpful

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); ;
    }
}
Ammar Khan
  • 2,565
  • 6
  • 35
  • 60
  • I think you could find the bcc if your MailMessage is in scope. You just need to declare such that it can be seen in your ContinueWith. – Mr. B Aug 11 '15 at 15:34
  • I will make the MailMessage is in scope. But how would I get the failed email? – Ammar Khan Aug 11 '15 at 15:36
  • If you mean the email fails in the sense that it can't be delivered, you have to actually programmatically check for the POSTMASTER failure response. I've done this before. It is not super easy, but it can be done. – Mr. B Aug 11 '15 at 15:39
  • I can't find any clue in the exception. Could you provide a little bit more info how would I check for POSTMASTER failure response? – Ammar Khan Aug 11 '15 at 15:40
  • What is an "incorrect" email address? If you mean a malformed address simply `try` adding and `catch (FormatException)` – Alex K. Aug 11 '15 at 15:46

0 Answers0