Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i have to send one mail to multiple mailid's i have the list of mailid's in my database.how can i send like that.(ex:subscribing u can find in some websites) any one plz help me.
Posted

C#
try
            {
                SmtpClient client = new SmtpClient();
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.Credentials = new NetworkCredential("gmail ID","password");

                client.EnableSsl = true;
                MailAddress from = new MailAddress("Gmail ID");
                MailMessage msg = new MailMessage();
                msg.From = from;

                string s11 = "select Email ID from Table";
                SqlDataAdapter add = new SqlDataAdapter(s11, c.getcon());
                DataSet ds2 = new DataSet();
                add.Fill(ds2);

                if (ds2.Tables[0].Rows.Count != 0)
                {
                    arra = new string[ds2.Tables[0].Rows.Count];
                    for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                    {
                        arra[i] = System.Convert.ToString(ds2.Tables[0].Rows[i][0].ToString());
                    }
                    for (int i1 = 0; i1 < arra.Length; i1++)
                    {
                        msg.To.Add(arra[i1]);
                        msg.Subject = subject name;
                        msg.Body = body;
                        client.Send(msg);
                        Label3.Text = "Message Send Successfully";
                    }
                }
}
catch(Exception e1)
{
}


Enjoy it...
 
Share this answer
 
v2
Comments
dineshdena 12-May-12 1:36am    
SqlDataAdapter add = new SqlDataAdapter(s11, c.getcon()); will u plz tell me whAT c.getcon where u have decl that c??? and this line ///arra = new string[ds2.Tables[0].Rows.Count];/// showing error. i declare arra as int is it correct
dineshdena 14-May-12 22:49pm    
I tried like this by showing all the datas in gridview and from gridview im getting the data row by row and putting in textbox by using text box im taking the to address from that textbox.My problem is when i click the button only one data from the gridview is detected in textbox and mail sent to that mailid in textbox.how to get next next datas from gridview i used for look i dnt weather its right r not plz help me...(in this the textbox and gridview are not visible)
con.Open();
SqlCommand cmd = new SqlCommand("select * from emailsubscribe", con);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ada.Fill(dt);
if (dt.Rows.Count > 0)
{
for (i = 0; i < dt.Rows.Count; i++)
{
TextBox1.Text = GridView1.Rows[0].Cells[1].Text.ToString();
StringBuilder sb = new StringBuilder();
MailMessage message = new MailMessage();
message.From = new MailAddress(TextBox1.Text);
message.To.Add(new MailAddress(TextBox1.Text));
message.Subject = "mail subscribe";
message.Body = "welcome";
message.IsBodyHtml = true;
try
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
NetworkCredential credential = new NetworkCredential("id@gmail.com", "password");
client.Credentials = credential;
client.Send(message);



}
catch (Exception ex)
{
Label1.Text = ex.Message;

}

}
Steps:
1. Get the mail id list from database.
2. Go in a for loop with the number of mail id's
3. send email one by one to the mail-id's

Following tip for sending email: Sending an Email in C# with or without attachments: generic routine.[^]

Try!
 
Share this answer
 
Comments
dineshdena 14-May-12 22:50pm    
I tried like this by showing all the datas in gridview and from gridview im getting the data row by row and putting in textbox by using text box im taking the to address from that textbox.My problem is when i click the button only one data from the gridview is detected in textbox and mail sent to that mailid in textbox.how to get next next datas from gridview i used for look i dnt weather its right r not plz help me...(in this the textbox and gridview are not visible)
con.Open();
SqlCommand cmd = new SqlCommand("select * from emailsubscribe", con);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ada.Fill(dt);
if (dt.Rows.Count > 0)
{
for (i = 0; i < dt.Rows.Count; i++)
{
TextBox1.Text = GridView1.Rows[0].Cells[1].Text.ToString();
StringBuilder sb = new StringBuilder();
MailMessage message = new MailMessage();
message.From = new MailAddress(TextBox1.Text);
message.To.Add(new MailAddress(TextBox1.Text));
message.Subject = "mail subscribe";
message.Body = "welcome";
message.IsBodyHtml = true;
try
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
NetworkCredential credential = new NetworkCredential("id@gmail.com", "password");
client.Credentials = credential;
client.Send(message);



}
catch (Exception ex)
{
Label1.Text = ex.Message;

}

}
Just bind all mail Ids with a semicolon as a separator in a string and assign it in To list, that sends the mail to all the recipients at once. No need of sending the same mail for each one of them.
 
Share this answer
 
Comments
dineshdena 11-May-12 23:48pm    
will u plz provide code for that im a begginer i dnt have much knowledge in this
dineshdena 14-May-12 22:50pm    
I tried like this by showing all the datas in gridview and from gridview im getting the data row by row and putting in textbox by using text box im taking the to address from that textbox.My problem is when i click the button only one data from the gridview is detected in textbox and mail sent to that mailid in textbox.how to get next next datas from gridview i used for look i dnt weather its right r not plz help me...(in this the textbox and gridview are not visible)
con.Open();
SqlCommand cmd = new SqlCommand("select * from emailsubscribe", con);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ada.Fill(dt);
if (dt.Rows.Count > 0)
{
for (i = 0; i < dt.Rows.Count; i++)
{
TextBox1.Text = GridView1.Rows[0].Cells[1].Text.ToString();
StringBuilder sb = new StringBuilder();
MailMessage message = new MailMessage();
message.From = new MailAddress(TextBox1.Text);
message.To.Add(new MailAddress(TextBox1.Text));
message.Subject = "mail subscribe";
message.Body = "welcome";
message.IsBodyHtml = true;
try
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
NetworkCredential credential = new NetworkCredential("id@gmail.com", "password");
client.Credentials = credential;
client.Send(message);



}
catch (Exception ex)
{
Label1.Text = ex.Message;

}

}

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