我在SQL Server中存储了多个电子邮件收件人。当我点击发送在网页上,它应该发送电子邮件给所有收件人。我使用;
分隔电子邮件。
以下是单个收件人的代码。
MailMessage Msg = new MailMessage();
MailAddress fromMail = new MailAddress(fromEmail);
Msg.From = fromMail;
Msg.To.Add(new MailAddress(toEmail));
if (ccEmail != "" && bccEmail != "")
{
Msg.CC.Add(new MailAddress(ccEmail));
Msg.Bcc.Add(new MailAddress(bccEmail));
}
SmtpClient a = new SmtpClient("smtp server name");
a.Send(Msg);
sreader.Dispose();
发布于 2014-05-06 10:09:35
简单!
只需在";“字符上拆分传入地址列表,并将它们添加到邮件中:
foreach (var address in addresses.Split(new [] {";"}, StringSplitOptions.RemoveEmptyEntries))
{
mailMessage.To.Add(address);
}
在本例中,addresses
包含"address1@example.com;address2@example.com
“。
发布于 2015-06-01 14:49:44
正如Adam Miller在评论中所建议的,我将添加另一个解决方案。
MailMessage(String from,String to)构造函数接受逗号分隔的地址列表。所以,如果你碰巧有一个逗号(',')分隔的列表,用法就像这样简单:
MailMessage Msg = new MailMessage(fromMail, addresses);
在这种情况下,我们可以将“;”替换为“,”,并且仍然使用构造函数。
MailMessage Msg = new MailMessage(fromMail, addresses.replace(";", ","));
无论你喜欢这个还是被接受的答案,这取决于你。可以说,循环使意图更清晰,但这更简短,也不模糊。但是,如果您已经有一个逗号分隔的列表,我认为这是可行的方法。
发布于 2021-08-29 08:40:44
这就是我的工作原理:
var addresses = "firstemail@email.com;secondemail@email.com";
var fromAddress = new MailAddress("sender@email.com", "Sender");
const string fromPassword = "Password";
string subject = "[Message] Message Subject" ;
string body = "Message Body";
var smtp = new SmtpClient
{
Host = "smtp.email.com",
Port = 587, //or 25 depending on your smtp server config
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
foreach (var address in addresses.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
using (var message = new MailMessage(fromAddress.ToString(), address)
{
Subject = subject,
Body = body,
Priority = MailPriority.High
})
{
smtp.Send(message);
}
}
https://stackoverflow.com/questions/23484503
复制相似问题