2

I am trying to send emails to a single 'To' recipient, and a list of 'Bcc' recipients. The list of Bcc recipients is a list of string, and they are successfully being added to the mailMessage's Bcc collection, but not actually being sent. If I add the same list to the message's 'Cc' collection it works fine. Just not the Bcc collection. The code I'm using is this:

 public void SendEmailMessage(String FromAddress, String ToAddress, String Subject, String Body, List<String> CCAddress, List<String> BccAddress, String Filepath)
    {
        using (SmtpClient mailClient = new SmtpClient())
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(FromAddress);
            mailMessage.To.Add(new MailAddress(ToAddress));
            foreach (String _email in CCAddress)
            {
                mailMessage.CC.Add(new MailAddress(_email));
            }
            foreach (String _email in BccAddress)
            {
                mailMessage.Bcc.Add(new MailAddress(_email));
            }
            mailMessage.Priority = MailPriority.Normal;
            mailMessage.Subject = Subject;
            if (Filepath != string.Empty)
            {
                Attachment _attachment = new Attachment(Filepath, MediaTypeNames.Application.Octet);
                mailMessage.Attachments.Add(_attachment);
            }
            AlternateView plainTextView = AlternateView.CreateAlternateViewFromString(GetTextonly(Body), null, "text/plain");
            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
            mailMessage.AlternateViews.Add(plainTextView);
            mailMessage.AlternateViews.Add(htmlView);
            SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
            smtpClient.Send(mailMessage);
        }
    }

any ideas?

dobestar
  • 311
  • 3
  • 15

3 Answers3

3

one thing i didn't mention is that the mail is put in a pickup directory rather than sent direct. I found a blog which explains that bcc addresses aren't sent if using a pickup directory, and you can put them in the retry directory instead. This solved my problem with an easy fix: Unable to send Bcc using System.Net.Mail when specifying a Pickup Directory(Exchange 2007/Exchange 2010) in code

dobestar
  • 311
  • 3
  • 15
1

As a workaround, you could send your email explicitly to the BCC address.

After you've successfully sent your email:

      mailClient.Send(mailMessage);

Clear the To address collection, then add your BCC address as a To address, and resend.

       mailMessage.To.Clear();   // clear the existing To & Cc fields
       mailMessage.Cc.Clear();
       mailMessage.To.Add(new MailAddress("bcc@address.com","CopyAddress"));
       mailClient.Send(mailMessage);
GlennG
  • 2,982
  • 2
  • 20
  • 25
0

I created a test application and ran SmptForDev to capture any emails going out from my local IIS. I used the code below and it works fine. All I've really done to your code is tidy it up and it works fine. I also decompiled System.Net.Mail.SmtpClient to see what it does under the hood, the To address and Bcc addresses are all put into one collection, if one address is sending it's good to assume they all are.

public void SendEmailMessage(string fromAddress, string toAddress, string subject, string body, IEnumerable<string> ccAddress, IEnumerable<string> bccAddress, string filepath)
        {
            using (var mailClient = new SmtpClient())
            {
                var mailMessage = new MailMessage(fromAddress, toAddress);

                foreach (var email in ccAddress)
                {
                    mailMessage.CC.Add(new MailAddress(email));
                }

                foreach (var email in bccAddress)
                {
                    mailMessage.Bcc.Add(new MailAddress(email,"Matty Boy"));
                }

                mailMessage.Priority = MailPriority.Normal;
                mailMessage.Subject = subject;

                if (!string.IsNullOrEmpty(filepath))
                {
                    var attachment = new Attachment(filepath, MediaTypeNames.Application.Octet);
                    mailMessage.Attachments.Add(attachment);
                }

                var plainTextView = AlternateView.CreateAlternateViewFromString(GetTextonly(body), null, "text/plain");
                var htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
                mailMessage.AlternateViews.Add(plainTextView);
                mailMessage.AlternateViews.Add(htmlView);

                mailClient.Send(mailMessage);
            }
        }

enter image description here

matt_lethargic
  • 2,706
  • 1
  • 18
  • 33