Wednesday, March 04, 2015

How To Send Multiple Emails In DotNet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;

public partial class MultipleEmails : System.Web.UI.Page
{
   SqlConnection con = new SqlConnection(@"Data Source=AAPTI-E86BE99D7\SQLEXPRESS;Initial Catalog=MultipleEmails;Integrated Security=True");
   SqlCommand cmd = new SqlCommand();
   DataSet ds = new DataSet();
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           Gridview();
       }
   }
   public void Gridview()
   {
       con.Open();

       SqlDataAdapter ad = new SqlDataAdapter("select * from Users", con);
       ad.Fill(ds);
       con.Dispose();
       GridView1.DataSource = ds;
       GridView1.DataBind();
       con.Close();
   }
   protected void btnSend_Click(object sender, EventArgs e)
   {
       con.Open();
       string email;
       DataTable dt = new DataTable();
       try
       {
           foreach (GridViewRow row in GridView1.Rows)
           {
               if (row.RowType == DataControlRowType.DataRow)
               {
                   CheckBox chk = (CheckBox)row.Cells[3].FindControl("CheckBox1");
                   if (chk.Checked == true)
                   {
                       if (chk != null && chk.Checked)
                       {
                           email = row.Cells[2].Text;
                           SmtpClient smtp = new SmtpClient();
                           smtp.Credentials = new NetworkCredential("prasadtarak1@gmail.com", "mavarick");
                           smtp.Port = 587;
                           smtp.Host = "smtp.gmail.com";
                           smtp.EnableSsl = true;
                           MailMessage msg = new MailMessage();
                           msg.From = new MailAddress("prasadtarak1@gmail.com");
                           msg.To.Add(email);
                           msg.Subject = "Hi frnds i Send a mail.";
                           msg.Body = "How are You.";
                           smtp.Send(msg);



                       }
                       else
                       {
                           Response.Write("<script>alert('Emails are sent Successfully.')</script>");
                       }
                   }

               }
           }
       }
       catch (Exception ex)
       {
           Response.Write("<script>alert('Failed.')</script>");
       }
   }



   protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
   {
       CheckBox chkAll =
           (CheckBox)GridView1.HeaderRow.FindControl("CheckBox2");
       if (chkAll.Checked == true)
       {
           foreach (GridViewRow gvRow in GridView1.Rows)
           {
               CheckBox chksel =
                   (CheckBox)gvRow.FindControl("CheckBox1");
               chksel.Checked = true;
           }
       }
       else
       {
           foreach (GridViewRow gvRow in GridView1.Rows)
           {
               CheckBox chksel =
                   (CheckBox)gvRow.FindControl("CheckBox1");
               chksel.Checked = false;
           }
       }
               
   }
}

No comments:

Post a Comment