Sending Emails with BCC list not working

流过昼夜 提交于 2020-01-24 23:31:35

问题


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?


回答1:


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




回答2:


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);
            }
        }




回答3:


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);


来源:https://stackoverflow.com/questions/23992114/sending-emails-with-bcc-list-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!