Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
642 views
in Technique[技术] by (71.8m points)

.net - 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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).

Source

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 (",").

MSDN page

so creating the MailMessage with a comma separated list should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...