无法使用 C# 向多个地址/收件人发送电子邮件 [英] Unable to send an email to multiple addresses/recipients using C#
问题描述
我正在使用以下代码,它只发送一封电子邮件 - 我必须将电子邮件发送到多个地址.
为了收到不止一封电子邮件,我使用:
string connectionString = ConfigurationManager.ConnectionStrings["email_data"].ConnectionString;OleDbConnection con100 = 新 OleDbConnection(connectionString);OleDbCommand cmd100 = new OleDbCommand("从bulk_tbl中选择前3封邮件", con100);OleDbDataAdapter da100 = 新 OleDbDataAdapter(cmd100);数据集 ds100 = 新数据集();da100.Fill(ds100);for (int i = 0; i < ds100.Tables[0].Rows.Count; i++)//尝试{字符串 all_emails = ds100.Tables[0].Rows[i][0].ToString();{string allmail = all_emails + ";";Session.Add("ad_emails",allmail);Response.Write(Session["ad_emails"]);发送邮件();}}
以及发送我使用的电子邮件:
string sendto = Session["ad_emails"].ToString();MailMessage message = new MailMessage("info@abc.com", sendto, "subject", "body");SmtpClient emailClient = new SmtpClient("mail.smtp.com");System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("abc", "abc");emailClient.UseDefaultCredentials = true;emailClient.Credentials = SMTPUserInfo;emailClient.Send(message);
问题是当 MailMessage
构造函数只接受一个字符串时,你提供了一个由分号分隔的地址列表表示单个地址:
包含电子邮件收件人地址的字符串.
或者可能是用逗号分隔的列表(见下文).
要指定多个地址,您需要使用 To
属性,它是一个 MailAddressCollection
,虽然这些页面上的示例没有很清楚地显示它:
message.To.Add("one@example.com, two@example.com"));
<块引用>
要添加到 MailAddressCollection 的电子邮件地址.多个电子邮件地址必须用逗号字符 (",") 分隔.
因此创建带有 逗号 分隔列表的 MailMessage
应该可以工作.
I am using the below code, and it only sends one email - I have to send the email to multiple addresses.
For getting more than one email I use:
string connectionString = ConfigurationManager.ConnectionStrings["email_data"].ConnectionString;
OleDbConnection con100 = new OleDbConnection(connectionString);
OleDbCommand cmd100 = new OleDbCommand("select top 3 emails from bulk_tbl", con100);
OleDbDataAdapter da100 = new OleDbDataAdapter(cmd100);
DataSet ds100 = new DataSet();
da100.Fill(ds100);
for (int i = 0; i < ds100.Tables[0].Rows.Count; i++)
//try
{
string all_emails = ds100.Tables[0].Rows[i][0].ToString();
{
string allmail = all_emails + ";";
Session.Add("ad_emails",allmail);
Response.Write(Session["ad_emails"]);
send_mail();
}
}
and for sending the email I use:
string sendto = Session["ad_emails"].ToString();
MailMessage message = new MailMessage("info@abc.com", sendto, "subject", "body");
SmtpClient emailClient = new SmtpClient("mail.smtp.com");
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("abc", "abc");
emailClient.UseDefaultCredentials = true;
emailClient.Credentials = SMTPUserInfo;
emailClient.Send(message);
The problem is that you are supplying a list of addresses separated by semi-colons to the MailMessage
constructor when it only takes a string representing a single address:
A String that contains the address of the recipient of the e-mail message.
or possibly a list separated by commas (see below).
To specify multiple addresses you need to use the To
property which is a MailAddressCollection
, though the examples on these pages don't show it very clearly:
message.To.Add("one@example.com, two@example.com"));
The e-mail addresses to add to the MailAddressCollection. Multiple e-mail addresses must be separated with a comma character (",").
so creating the MailMessage
with a comma separated list should work.
这篇关于无法使用 C# 向多个地址/收件人发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!