无法使用C#向多个地址/收件人发送电子邮件 [英] Unable to send an email to multiple addresses/recipients using C#
问题描述
我正在使用以下代码,它只发送一封电子邮件 - 我必须将邮件发送到多个地址。
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);
推荐答案
问题是您提供的地址列表用分号分隔成 MailMessage
构造函数,只需要一个代表单个地址的字符串:
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
属性,这是一个 MailAddressCollection
,虽然这些页面上的示例不能很清楚地显示:
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"));
要添加到MailAddressCollection的电子邮件地址。多个电子邮件地址必须用逗号分隔(,)。
The e-mail addresses to add to the MailAddressCollection. Multiple e-mail addresses must be separated with a comma character (",").
所以创建 MailMessage
与逗号分隔列表应该可以工作。
so creating the MailMessage
with a comma separated list should work.
这篇关于无法使用C#向多个地址/收件人发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!