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

本文关键字:电子邮件 收件人 MailMessage 使用 | 更新日期: 2023-09-27 18:01:54

我有多个电子邮件收件人存储在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();

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

轻松!

只需在";"字符上分割接收地址列表,并将它们添加到邮件消息中:

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

本例中addresses包含" address1@example.com;address2@example.com "

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

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

MailMessage Msg = new MailMessage(fromMail, addresses);

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

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

你喜欢这个还是接受的答案取决于你。可以说,循环使意图更清晰,但这是更短,而不是模糊。但是如果你已经有一个逗号分隔的列表,我认为这是一种方法。

根据文档:

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/

我是这样做的:

            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);
                }
            }

您可以使用LINQ:

string toEmail = "name1@mydomain.com;name1@mydomain.com";
toEmail.Split(";").ToList().ForEach(address => Msg.To.Add(new MailAddress(address)));

我使用以下powershell脚本并在地址之间使用(,)进行了测试。这对我很有效!

$EmailFrom = "<from@any.com>";
$EmailPassword = "<password>";
$EmailTo = "<to1@any.com>,<to2@any.com>";
$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);