我无法想出以下内容.如何使用以下代码将电子邮件发送到多个电子邮件地址?
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "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);
}
Run Code Online (Sandbox Code Playgroud)
使用MailMessage不带参数的构造函数,然后分配From属性.该To属性实际上是一个集合,可以让您添加任意数量的人:
using (var message = new MailMessage())
{
message.From = fromAddress;
message.To.Add(new MailAddress("to1@example.com", "To One"));
message.To.Add(new MailAddress("to2@example.com", "To Two"));
}
Run Code Online (Sandbox Code Playgroud)