我有一个函数可以获取地址列表,并使用google 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("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);
}
}
}
答案 0 :(得分:4)
使用BCC添加“密件抄送”收件人 - 他们会收到邮件的副本,而不会看到其他BCC的电子邮件地址。
for (int i = 0; i < address_l.Count; i++)
{
message.Bcc.Add(new MailAddress(address_l[i], names[i]));
}