0

When i used this code, when the email is delivered the list of addresses in bcc is visible, why please?

I use .Net Framework 4.6.2

The code works correctly, it sends the emails but when I check the To: in the email delivered I can see all the recipients that I have included in .Bcc.Add

bcc does not work as microsoft says?

    public static bool SendEmails(string[] emailList, string from, string body, string subject, string attachment)
    {
            var result= false;
            MailMessage email = null;
                        
            if (emailList!= null && !string.IsNullOrWhiteSpace(from))
            {
                    email = new MailMessage
                    {
                        Priority = MailPriority.High,
                        From = new MailAddress(from),
                        Subject = subject,
                        Body = body,
                        BodyEncoding = Encoding.UTF8,
                        IsBodyHtml = true,
                        DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure,
                    };

                    if (!string.IsNullOrWhiteSpace(attachment))
                    {
                        email.Attachments.Add(new Attachment(attachment));
                    }

                    var smtp = new SmtpClient
                    {
                        Host = host,
                        Credentials =new System.Net.NetworkCredential("user","pass"),
                        EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["enablessl"]),
                        Port = int.Parse(ConfigurationManager.AppSettings["port"])
                    };


                    if (emailList.Count() > 0)
                    {
                        foreach (string email in emailList)
                        {
                            email.Bcc.Add(new MailAddress(email));  
                        }
                    }

 smtp.Send(email);
                    result= true;
             }
return restul;
}


JG73
  • 90
  • 1
  • 10
  • `SmtpClient` doesn't have a `Bcc` property, `MailMessage` does though https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage.bcc?view=net-6.0 so what is your *real* code? – Charlieface Jul 12 '22 at 08:38
  • You think a widely used feature is just plain broken and nobody noticed? Please post a *complete* [mcve] and confirm *where* and *how* you're checking the email content after sending to observe the email addresses, – Damien_The_Unbeliever Jul 12 '22 at 08:39
  • After a thousand tests is what I have thought, how is it possible that this feature does not work for years, I do not understand... – JG73 Jul 12 '22 at 09:32
  • 1
    When you say you're looking at the sent email, you mean the one in your sent items somewhere? That one *will* continue to store the BCC values. It is not the same as what the *recipient* will see in their copy of the email, which is what you should be looking at. – Damien_The_Unbeliever Jul 12 '22 at 09:41
  • 1º I have a list of emails, "me@dot.com,one@dot.com, two@dto.com, three@dot.com" 2º I send all emails as bcc, as seen in the code 3º When the emails arrive to the users, any user who has received it can see all the recipients in the To: "me@dot.com, one@dot.com, two@dto.com, three@dot.com" and not should see them (this is the problem), Thankyou for your time @Damien_the_Unbeliever – JG73 Jul 12 '22 at 10:00
  • Sorry Damien_The_Unbeliever, i mean , when i check email delivered the "To:" have all email addresses – JG73 Jul 12 '22 at 10:21
  • Which SMTP server are you using and what is the email client application where you verify this behaviour? – Danish Jul 12 '22 at 10:35
  • The smtp is that of my company, and the email in which I see the behavior is one of company tests and another of gmail tests, in both I see the addresses that I have sent as "bcc" – JG73 Jul 12 '22 at 11:00
  • client gmail web, and outllook desktop – JG73 Jul 12 '22 at 11:25
  • when i send only email.To.add("somemail@dot.com"); email.Bcc.Add("anothermail@pp.com") ; smtp.send(email), works, no show bcc adresesses, the problem is the loop foreach, for some reason when i add bcc in loop not work and show all adrresses :( – JG73 Jul 12 '22 at 12:13
  • 1
    Try setting `email.To` as your own email address – Charlieface Jul 12 '22 at 12:45
  • Thanks so much Charlieface, works, It's the only way it seems, if email.to is outside of loop. – JG73 Jul 12 '22 at 13:17
  • Do you know if this could have other implications such as emails ending up in the spam folder or something like that? – JG73 Jul 12 '22 at 14:43
  • Don't know, but it's almost certainly nothing to do with `SmtpClient` and everything to do with your server. Speak to Google or other provider – Charlieface Jul 12 '22 at 22:39

1 Answers1

0

Apparently, it is necessary to include an email in emai.To.Add("anymail@anyhost.com") before starting the loop where the addresses are loaded in bcc, I don't know if this has any other consequences, but that's how it works. Email addresses are no longer displayed in the To: of the delivered message.

JG73
  • 90
  • 1
  • 10