我无法想出以下内容。如何使用以下代码将电子邮件发送到多个电子邮件地址?
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);
}
答案 0 :(得分:5)
使用不带参数的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"));
}
答案 1 :(得分:1)
怎么样:
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from@gmail.com", "From 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()
{
From = fromAddress,
Subject = subject,
Body = body
})
{
message.To.Add(new MailAddress("recipient1@example.com", "Name"));
message.To.Add(new MailAddress("recipient2@example.com", "Name"));
smtp.Send(message);
}
答案 2 :(得分:0)
您可以使用任何To
对象的protery MailMessage
:
message.To.Add(new MailAddress("abc@def.com","ABC"));