Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to send an email to multiple addresses/recipients using C#

Tags:

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("[email protected]", 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); 
like image 655
azeem Avatar asked Jul 09 '10 00:07

azeem


People also ask

How do I send an email to multiple recipients using Cc?

To send emails to small groups where everybody knows each other, use the Cc field. Enter all of the addresses there, separated by commas. To hide addresses, use the Bcc field, just like the Cc field. No one will be able to see the addresses added in this field.

How do I send email to multiple users individually?

Open a new email and write the message you intend to send to your contact list. Click BCC in the top-right of your Compose window. Add all the email addresses to which you intend to send the message. It might help to copy and paste your list into this field.

How do I send to multiple email addresses in SMTP?

In short, to send to multiple recipients you should set the header to be a string of comma delimited email addresses. The sendmail() parameter to_addrs however should be a list of email addresses.

Can I use Bcc to send mass email?

Can I use BCC to send mass emails? Yes. You can use BCC to send mass emails by putting multiple recipients in the To: field and using BCC to hide them from each other's inboxes so they don't know they're all receiving the same message.


2 Answers

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("[email protected], [email protected]")); 

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.

like image 182
ChrisF Avatar answered Oct 09 '22 07:10

ChrisF


This is what worked for me. (recipients is an Array of Strings)

//Fuse all Receivers var allRecipients = String.Join(",", recipients);  //Create new mail var mail = new MailMessage(sender, allRecipients, subject, body);  //Create new SmtpClient var smtpClient = new SmtpClient(hostname, port);  //Try Sending The mail try {     smtpClient.Send(mail); } catch (Exception ex) {     Log.Error(String.Format("MailAppointment: Could Not Send Mail. Error = {0}",ex), this);     return false; } 
like image 41
VRC Avatar answered Oct 09 '22 07:10

VRC