使用MailMessage将电子邮件发送给多个收件人? [英] Send Email to multiple Recipients with MailMessage?

查看:106
本文介绍了使用MailMessage将电子邮件发送给多个收件人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server中存储了多个电子邮件收件人。当我单击网页中的发送时,它将发送电子邮件给所有收件人。我使用; 分隔了电子邮件。

I have multiple email recipients stored in SQL Server. When I click send in the webpage it should send email to all recipients. I have separated emails using ;.

以下是单个收件人代码。

Following is the single recipient code.

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


推荐答案

简单!

只需将传入地址列表拆分为;字符,并将其添加到邮件中:

Just split the incoming address list on the ";" character, and add them to the mail message:

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

在此示例中,地址包含 address1@example.com; address2@example.com

这篇关于使用MailMessage将电子邮件发送给多个收件人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆