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);

            }

        }

    }

}

Gridview to email

 using System;

using System.IO;

using System.Net;

using System.Net.Mail;

using System.Text;

using System.Web.UI;

using System.Web.UI.WebControls;


protected void btnSendEmail_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);

            }

        }


        string gridHtml = sb.ToString();


        // Create email

        MailMessage mail = new MailMessage();


        mail.From = new MailAddress("yourmail@gmail.com");

        mail.To.Add("receiver@gmail.com");


        mail.Subject = "GridView Report";

        mail.Body = @"

            <html>

            <body>

                <h2>GridView Report</h2>

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

                " + gridHtml + @"

            </body>

            </html>";


        mail.IsBodyHtml = true;


        // SMTP settings

        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);


        smtp.EnableSsl = true;

        smtp.Credentials = new NetworkCredential(

            "yourmail@gmail.com",

            "YOUR_APP_PASSWORD"

        );


        smtp.Send(mail);


        Response.Write("<script>alert('Email sent successfully');</script>");

    }

    catch (Exception ex)

    {

        Response.Write("<script>alert('" +

            ex.Message.Replace("'", "") +

            "');</script>");

    }

}





Main code 

public override void VerifyRenderingInServerForm(Control control)

{

    // Required for GridView rendering

}


Hyper link control with EVAL PATH

<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("name") %>'  


NavigateUrl='<%# "~/news/" + Eval("filen") %>' ForeColor="Wheat"> </asp:HyperLink>


FILE UPLOAD MAX SIZE WITH SERVER FOLDER

 WEB CONFIG   FILE 

<?xml version="1.0"?>

<configuration>

  <system.web>



    <httpRuntime maxRequestLength="1048576" executionTimeout="999999"/>


    <customErrors mode="Off"></customErrors>

    <compilation debug="true" targetFramework="4.0"/>

  </system.web>

</configuration>


Folder Code 

 protected void Button1_Click(object sender, EventArgs e)

    {

        //file upload 


        FileUpload1.SaveAs(Server.MapPath(@"~/nsadmin/") + "//news//" + FileUpload1.FileName);

        Label1.Text = "File upload successfully please click save button to save record";

    }







Gridview send email

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