Tuesday 5 March 2013

Send GridView in Mail in asp.net,C#


Send GridView in Mail in asp.net,C#

http://dotnetknowledgebox.blogspot.in/2012/04/send-gridview-in-mail-in-aspnetc.html

This example makes u enable to understand how to email server controls information like Gridview as it is.
Now I am considering that we have a web form which contains a GridView (containing Data) and a button
which will be used to send email.

At first include these namespaces to your code behind.

using System.Net.Mail;
using System.Text;
using System.IO;

Now in Button Click event write this code :


protected void ibMail_Click(object sender, ImageClickEventArgs e)
    {
        string to = "anukana@symtechindia.net";
        string From = "mafire5@gmail.com";
        string subject = "Balance Detail";
        string Body = "Dear sir ,<br> Plz Check d Attachment <br><br>";
        Body += GridViewToHtml(gvPArtyStatement); //Elaborate this function detail later
        Body += "<br><br>Regards,<br>Anukana";
        bool send = send_mail(to, From, subject, Body);//Elaborate this function detail later
        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);
         }
    }

send_mail() Definition :

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("jhalpharegi@gmail.com", "Your_password");
            client.EnableSsl = true; //Gmail works on Server Secured Layer
            client.Send(msg);
            bool k = true;
            return k;
   }


GridViewToHtml() definition : 


 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.
    }


Now browse and send mail and output will be like this one -: 


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.

1 comment:

  1. Blogs often encourage reader engagement through comments and social media shares Energy And Industry fostering a sense of community and two-way communication.

    ReplyDelete