Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am building the webservice. I need to send email logically through code.
For example : The User will send a request to webserver. He will choose Multiple recipients by selecting the check boxes. The webserver will get the recipients name and email id list. So please help me that, through code i will be able to send a mail to multiple recipients.

Thanks in advance.
Posted
Updated 8-Sep-17 20:58pm

There is a generic routine here for sending email: Sending an Email in C# with or without attachments: generic routine.[^]
All you have to do is add more "To" addresses, or use the CC / BCC list instead.
 
Share this answer
 
Comments
Manas Bhardwaj 19-Jun-12 6:25am    
Nice that you added it as a tip. Wonder that people have stopped using Google and instead ask questions at CP. :)
My 5!
rp786 28-Jun-12 6:06am    
Google gives all the solution but Code project gives the best solution out of all the solutions. So people ask question at CP...
Try this:

Namespaces:
C#
using System.Collections; //ArrayList
using System.Data.SqlClient; //SQL
using System.Net.Mail; //email

Code:
C#
private void btnSend_Email_Click(object sender, EventArgs e)
        {
            ArrayList list_emails = new ArrayList();
            int i = 0, email = 0;
            sqlConnection1.Open(); //connection to the database.
            SqlCommand cmd_Email = new SqlCommand("Select Email from Email_Table", sqlConnection1);
            SqlDataReader read_Email = cmd_Email.ExecuteReader();
            while (read_Email.Read())
            {
                email = read_Email.GetValue(i).ToString();
                list_emails.Add(email); //Add email to a arraylist
                i = i + 1 - 1; //increment or ++i
            }
            read_Email.Close();
            sqlConnection1.Close(); //Close connection
 
            foreach (string email_to in list_emails)
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(email_to);
                mail.Subject = "Welcome to C#";
                mail.From = new MailAddress(hold_Account);
                mail.Body = "Test";
                SmtpClient smtp = new SmtpClient("SMTP Server");
                smtp.Send(mail);
            }

Ref.: send email to multiple email addresses [^]
 
Share this answer
 
Comments
Manas Bhardwaj 19-Jun-12 6:24am    
correct +5!
Prasad_Kulkarni 19-Jun-12 8:35am    
Thank you Manas!
Stelig 30-Mar-15 9:26am    
I am trying to implement a solution like: use a checkbox named "All customers" and if I check it, I send e-mail to all customers addresses.

I have tried the code just as Prasad_Kulkarni posted (modified the sql statement) and I receive the following errors:

1. email = read_Email.GetValue(i).ToString();
->Cannot implicitly convert type 'string' to 'int'

2. mail.From = new MailAddress(hold_Account);
->The name 'hold_Account' does not exist in the current context

Can someone help with this, please?
Member 11970059 17-Mar-16 12:42pm    
Did u get a solution to your 1st problem... I m having the same problem.
Member 11970059 17-Mar-16 12:41pm    
read_Email.GetValue(i).ToString();
This above line is showing an error that cannot implicitly convert type string to int.

Plz help asap...
You can have all the email ids comma separated in the to address. And then send you will be able to send for multiple recipients.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900