2013-05-29 58 views
0

我有一個函數,它獲取地址列表併發送相同的電子郵件消息給列表中的每個聯繫人使用谷歌smtp。但我怕我的代碼效率不高,因爲它發送了幾封單獨的郵件。 如何將更多聯繫人添加到同一客戶端並一次發送所有聯繫人?發送電子郵件給多個聯繫人

這裏是我的功能:

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("[email protected]", "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); 
      } 
     } 

    } 

回答

4

使用BCC添加「密件抄送」收件人 - 他們會收到郵件的副本,沒有看到其他BCC'ed電子郵件地址。

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