使用 MailMessage 向多个收件人发送电子邮件?

问题描述 投票:0回答:6

我在 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();
c# mailmessage
6个回答
215
投票

简单!

只需将传入地址列表拆分在“;”上即可字符,并将它们添加到邮件消息中:

foreach (var address in addresses.Split(new [] {";"}, StringSplitOptions.RemoveEmptyEntries))
{
    mailMessage.To.Add(address);    
}

在此示例中,

addresses
包含“
[email protected];[email protected]
”。


102
投票

根据 Adam Miller 在评论中的建议,我将添加另一个解决方案。

MailMessage(String from, String to) 构造函数接受逗号分隔的地址列表。因此,如果您碰巧已经有一个逗号(',')分隔的列表,那么用法就很简单:

MailMessage Msg = new MailMessage(fromMail, addresses);

在这种特殊情况下,我们可以替换“;” for ',' 并且仍然使用构造函数。

MailMessage Msg = new MailMessage(fromMail, addresses.replace(";", ","));

您是否喜欢这个答案或接受的答案取决于您。可以说,循环使意图更清晰,但它更短且不晦涩。但是如果您已经有一个逗号分隔的列表,我认为这是正确的方法。


12
投票

根据文档:

MailMessage.To 属性 - 返回包含此电子邮件的收件人列表的 MailAddressCollection

这里

MailAddressCollection 有一个名为 的内置方法

public void Add(string addresses) 1. Summary: Add a list of email addresses to the collection. 2. Parameters: addresses: *The email addresses to add to the System.Net.Mail.MailAddressCollection. Multiple *email addresses must be separated with a comma character (",").

因此,如果有多个收件人,则要求:

传递包含以逗号分隔的电子邮件地址的字符串

您的情况:

只需替换所有 ;与 ,

Msg.To.Add(toEmail.replace(";", ","));

供参考:

  1. https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage?view=netframework-4.8
  2. https://www.geeksforgeeks.org/c-sharp-replace-method/

1
投票
这对我来说是有效的:

var addresses = "[email protected];[email protected]"; var fromAddress = new MailAddress("[email protected]", "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); } }
    

0
投票
您可以使用 LINQ:

string toEmail = "[email protected];[email protected]"; toEmail.Split(";").ToList().ForEach(address => Msg.To.Add(new MailAddress(address)));
    

-5
投票
我已经使用以下 powershell 脚本并在地址之间使用 (,) 对此进行了测试。这对我有用!

$EmailFrom = "<[email protected]>"; $EmailPassword = "<password>"; $EmailTo = "<[email protected]>,<[email protected]>"; $SMTPServer = "<smtp.server.com>"; $SMTPPort = <port>; $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer,$SMTPPort); $SMTPClient.EnableSsl = $true; $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($EmailFrom, $EmailPassword); $Subject = "Notification from XYZ"; $Body = "this is a notification from XYZ Notifications.."; $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body);
    
© www.soinside.com 2019 - 2024. All rights reserved.