Friday, August 28, 2026

Gridview send email

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.IO;

using System.Text;

using System.Net.Mail;


namespace connectivity

{

    public partial class grid_email : System.Web.UI.Page

    {

        SqlConnection con;

        SqlCommand com;

        SqlDataReader dr;


        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                display();

            }

        }

        protected void display()

        {

            //select query Implementation//

            string path = @"Data Source=DESKTOP-9KU60QI;Initial Catalog=college;Integrated Security=True";

            con = new SqlConnection(path);

            con.Open();


            string a = "select * from dbconnect";

            com = new SqlCommand(a, con);


            dr = com.ExecuteReader();


            GridView1.DataSource = dr;

            GridView1.DataBind();


            dr.Close();


        }

        public override void VerifyRenderingInServerForm(Control control)

        {

            // Required for GridView rendering

        }


        protected void Button1_Click(object sender, EventArgs e)

        {

            try

            {

                // Convert GridView to HTML

                StringBuilder sb = new StringBuilder();


                using (StringWriter sw = new StringWriter(sb))

                {

                    using (HtmlTextWriter hw = new HtmlTextWriter(sw))

                    {

                        GridView1.RenderControl(hw);

                    }

                }

                //email programming//

                string gridhtml = sb.ToString();


                //create mail object//

                MailMessage msg = new MailMessage();

                //set property//

                msg.To.Add(TextBox1.Text);

                msg.From = new MailAddress("vrushalihatewar@gmail.com");

                msg.Subject = "This is testing mail";


                msg.Body = @"

                <html>

                <body>

                    <h2>GridView Report</h2>

                    <p>Please find the report below:</p>

                    " + gridhtml + @"

                </body>

                </html>";




                //set mail format//

                msg.IsBodyHtml = true;



                //create smtp client object//

                SmtpClient stp = new SmtpClient();

                //set the smtp property//

                stp.Host = "smtp.gmail.com";

                stp.Credentials = new System.Net.NetworkCredential("vrushalihatewar@gmail.com", "uzvq dloh ljjk nonh");

                stp.EnableSsl = true;


                //send mail//

                stp.Send(msg);

                Response.Write("mail sent sucessfully");


            }


            catch(Exception ex)

            {

                Response.Write(ex);

            }

        }

    }

}

No comments:

Post a Comment

Gridview send email

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; ...