我有一个函数,它获取地址列表并使用 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);
}
}
}