Tuesday, 30 October 2012

Sending GridView Data in Mail


Sending GridView Data in Mail

Today I will show to Send Gridview Data in mail... Its very simple.. Lets give it a try..

Prerequisites:

A Populated Gridview in your design part with code for populating it already present in the codebehind..

A Button named Send Mail which will send the gridview data using email..


Getting Started:

Include these namespaces in your code-behind:


using System.Net.Mail;

using System.Text;

using System.IO;


Then On Button Click Event:


    protected void SendMail_Click(object sender, ImageClickEventArgs e)

    {

        //Specify the Email Address of the Recepient

        string to = "abc@xyz.com";

        //Specify the Email Address of the Sender

        string From = "xyz@abc.com";

        //Insert the subject of the mail

        string subject = "GridView Data";

     

        //Specifies the content of the body

        string Body = "Dear sir ,<br> Plz Check d Attachment <br><br>";

        Body += GridViewToHtml(Gridview1);

        Body += "<br><br>Regards,<br>Vishal";

        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...try later!');";

            ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);

        }

    }


I have used a couple of methods in above event handler.. You have to specify those methods in the codebehind:

Boolean Send Mail Method:


    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("xyz@abc.com", "Your_password");

            client.EnableSsl = true; //Gmail works on Server Secured Layer

            client.Send(msg);

            bool k = true;

            return k;

    }


Gridview To Html Method:


    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.

    }


Note(Important):

1> You have to preconfigure your gmail settings(POP and IMAP) in order to send and receive mail..

2>You have to preconfigure you web.config file for sending mail.. If you dont know google that..

3> Important

Sometime one can  encountered by this error  -:
RegisterForEventValidation can only be called during Render();

This means that either you have forgot to override VerifyRenderingInServerForm in code behind or EventValidation is true.
So the solution is set EventValidation to false and must override VerifyRenderingInServerForm method.

4> If Everything worked fyn you would be able to Send your Gridview via Mail...

:-)

No comments:

Post a comment

Thank You for Your Comments. We will get back to you soon.

back to top