Sending to multiple Email addresses but displaying only one C#

风流意气都作罢 提交于 2020-01-14 09:07:03

问题


I am using the SmtpClient in C# and I will be sending to potentially 100s of email addresses. I don't want to have to loop through each one and send them an individual email.

I know it is possible to only send the message once but I don't want the email from address to display the 100s of other email addresses like this:

Bob Hope; Brain Cant; Roger Rabbit;Etc Etc

Is it possible to send the message once and ensure that only the recipient's email address is displayed in the from part of the email?


回答1:


Ever heard of BCC (Blind Carbon Copy) ? :)

If you can make sure that your SMTP Client can add the addresses as BCC, then your problem will be solved :)

There seems to be a Blind Carbon Copy item in the MailMessage class

http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx

http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.bcc.aspx

Here is a sample i got from MSDN

public static void CreateBccTestMessage(string server)
        {
            MailAddress from = new MailAddress("ben@contoso.com", "Ben Miller");
            MailAddress to = new MailAddress("jane@contoso.com", "Jane Clayton");
            MailMessage message = new MailMessage(from, to);
            message.Subject = "Using the SmtpClient class.";
            message.Body = @"Using this feature, you can send an e-mail message from an application very easily.";
            MailAddress bcc = new MailAddress("manager1@contoso.com");

                //This is what you need
                message.Bcc.Add(bcc);
                SmtpClient client = new SmtpClient(server);
                client.Credentials = CredentialCache.DefaultNetworkCredentials;
                Console.WriteLine("Sending an e-mail message to {0} and {1}.", 
                    to.DisplayName, message.Bcc.ToString());
          try {
            client.Send(message);
          }  
          catch (Exception ex) {
            Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}", 
                        ex.ToString() );
          }
        }



回答2:


If you are using the MailMessage class, make use of the BCC (Blind Carbon Copy) property.

MailMessage message = new MailMessage();
MailAddress bcc = new MailAddress("manager1@contoso.com"); 

// Add your email address to BCC
message.Bcc.Add(bcc);


来源:https://stackoverflow.com/questions/3219965/sending-to-multiple-email-addresses-but-displaying-only-one-c-sharp

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