简体   繁体   English

发送电子邮件给多个联系人

[英]send email to multiple contacts

I have a function which gets a list of addresses and send the same email message to every contact in the list using google smtp. 我有一个获取地址列表并使用google smtp向列表中的每个联系人发送相同电子邮件的功能。 but I affraid my code is not very efficient because it sends several seperate mails. 但我认为我的代码效率不高,因为它会发送多个单独的邮件。 how do I add more contacts to the same client and send all of them at once? 如何将更多联系人添加到同一客户端并一次发送所有联系人?

here is my function: 这是我的功能:

protected void btn_click(object sender, EventArgs e, List<string> address_l, List<string> names)
    {
        for (int i = 0; i < address_l.Count; i++)
        {
            var fromAddress = new MailAddress("dvir.rez@gmail.com", "Dvir");
            var toAddress = new MailAddress(address_l[i], names[i]);

            const string fromPassword = "2.4.862.4.86";
            const string subject = "test";
            string body = "Test body";

            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = body
            })
            {
                smtp.Send(message);
            }
        }

    }

Use BCC to add "blind carbon copy" recipients - they'll receive a copy of the message, without seeing other BCC'ed email addresses. 使用密件抄送添加“密件抄送”收件人-他们将收到邮件的副本,而不会看到其他密件抄送的电子邮件地址。

for (int i = 0; i < address_l.Count; i++)
{
   message.Bcc.Add(new MailAddress(address_l[i], names[i]));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM