Wednesday 26 February 2014

Widgets

Send Email to all user's email Id in the Database in Asp.net


Description:- Hello Friends Today we will learn that how to send email to all the user's email id in the database on Button click event.First of all we will create a button on design page.On the onclick event give a name to the event whatever you want.Second thing you have to make a Table in the database that is containing Column for email in which the email id for all the user are stored.i will also provide you snapshots for creating your Table in the database.the snapshot for database are given below.
Database Table:- Design Table according to your Data that you want to store in the database.
After Creating add the data to the table.Now the code for Design page is below.
Demo:-


Design Code:-

Code Behind:-
 protected void SendMail_Click(object sender, EventArgs e)
{
    cmd3 = new SqlCommand("select email from tbluser", con);
        string email;
        con.Open();
       SqlDataReader dr=cmd3.ExecuteReader();
       while(dr.Read())
       { 
           email=dr[0].ToString();
           MailMessage myMsg = new MailMessage();
           myMsg.From = new MailAddress("*****@gmail.com");
           myMsg.To.Add(email);
           myMsg.Subject = "This is Reminder Mail to you";
           myMsg.IsBodyHtml = true;
           string currentyear = DateTime.Now.Year.ToString();


           myMsg.Body = "Dear Pay your Membership Fee.If Already Paid Ignore this mail";



           myMsg.CC.Add("*********@gmail.com");
           // your remote SMTP server IP.
           SmtpClient smtp = new SmtpClient();
           smtp.Host = "smtp.gmail.com";
           smtp.Port = 587;
           smtp.Credentials = new System.Net.NetworkCredential("*******@gmail.com", "Password");
           smtp.EnableSsl = true;
           smtp.Send(myMsg);

           lblmail.Text = "Mail Sent";
       }
}

0 comments:

Post a Comment