Wednesday, 28 November 2012

GRIDVIEW MAIL USING ASP.NET C#

 IN THE ASPX PAGE

  <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
       </asp:GridView>
    </div>
        <asp:Button ID="btnSendMail" runat="server" Text="Send Gridview Mail"
        onclick="btnSendMail_Click" Width="123px" />
    <br />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </form>

IN THE ASXP.CS PAGE
//Name space to be used
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Net.Mail;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

public partial class Default : System.Web.UI.Page
{
    static string strcon = ConfigurationManager.ConnectionStrings["SQLCON"].ConnectionString;
    //create new sqlconnection and connection to database by using connection string from web.config file
    SqlConnection con = new SqlConnection(strcon);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridview();
        }
    }

    // This method is used to bind gridview from database
    protected void BindGridview()
    {
      
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from tablename", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
//Send mail button click event
    protected void btnSendMail_Click(object sender, EventArgs e)
    {
        string to = TextBox1.Text;//Gets the TO id
        string From = "fromid@gmail.com";
        string subject = "Mark Detail";
        string Body = "HI ,<br>  Check d Attachment <br><br>";
        Body += GridViewToHtml(GridView1);
        Body += "<br><br>Regards,<br>sathya";
        bool send = send_mail(to, From, subject, Body);
        if (send == true)
        {
            string CloseWindow = "alert('Mail Sent Successfully!');";
            ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
        }
        else
        {
            string CloseWindow = "alert('Problem in Sending mail');";
            ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
        }

    }
//Mail function
    public bool send_mail(string to, string from, string subject, string body)
    {
        MailMessage msg = new MailMessage(from, to);
        msg.Subject = subject;
        AlternateView view;
        SmtpClient client;
        StringBuilder msgText = new StringBuilder();
        msgText.Append(" <html><body><br></body></html> <br><br><br>  " + body);
        view = AlternateView.CreateAlternateViewFromString(msgText.ToString(), null, "text/html");

        msg.AlternateViews.Add(view);
        client = new SmtpClient();
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.Credentials = new System.Net.NetworkCredential("fromid@gmail.com", "fromidpassword");
        client.EnableSsl = true; //Gmail works on Server Secured Layer
        client.Send(msg);
        bool k = true;
        return k;
    }
//calling the html of the gridview
    private string GridViewToHtml(GridView gv)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        gv.RenderControl(hw);
        return sb.ToString();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
    }
}

No comments:

Post a Comment

back to top