Tuesday 28 May 2013

How to send Bulk email in asp.net using c#.

Introduction:-

In the previous article , I have have explained how to send email in asp.net using c#. In this article I have explained how to send bulk email in asp.net using c#. Suppose that you have 50 email and your want to send them all at a time.

Let's create a table in which you have the following fields..

 id, name, emailid,country, state,city
 Insert the data in the table.

Now if you want to send the mail on button click event if you are using sql database. then read the table and use for loop for taking email id one by one.

for connection:-

string constr = ConfigurationManager.ConnectionStrings["sushilconn"].ConnectionString;

Code on button click event:-

protected void Button1_Click(object sender, EventArgs e)

    {
        con.Open();
        MySqlCommand cmd = new MySqlCommand("Select emailid from details", con);
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        for (int i = 0;i< dt.Rows.Count;i++ )
        {
            string id=dt.Rows[i].ItemArray[0].ToString();
            MailMessage mail = new MailMessage();
            mail.To.Add(id);
            mail.From = new MailAddress("youremailid@gmail.com");
            mail.Subject = "Email using Gmail";

            string Body = "Hi, this mail is to test sending mail" +
                          "using Gmail in ASP.NET";
            mail.Body = Body;

            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
            smtp.Credentials = new System.Net.NetworkCredential
                 ("youremailid@gmail.com", "yourpassword");
            //Or your Smtp Email ID and Password
            smtp.EnableSsl = true;
            smtp.Send(mail);

        }
}

The Above code send the mail one by one.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.