Pages

Friday 21 March 2014

Send GridView in Mail throughSharepoint

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)
    {
       //Get the Sharepoint SMTP information from the SPAdministrationWebApplication  
                SPUser sUser = _SPHelper.CurrentWeb.CurrentUser;
                string FromEmailAddress = sUser.Email;
                string smtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
                string smtpFrom = false ? SPAdministrationWebApplication.Local.OutboundMailSenderAddress : FromEmailAddress;
                string subject = "GridView Details";
                string Body = "Dear Sir/Madam ,<br> Plz Check the gridview details <br><br>";
                Body += GetGridviewData(Grid1); //Elaborate this function detail later
                Body += "<br><br>Regards,<br>" + sUser.Name;
                bool send = send_mail("TestUser@gmail.com", smtpFrom, subject, Body, smtpServer);//Elaborate this function detail later
                if (send == true)
                {
                    ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "Insert", "alert('Mail has been sent.');location.href('" + _SPHelper.CurrentWeb.Site.Url + "');", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "Insert", "alert('Problem in sending mail....Try Later...');location.href('" + _SPHelper.CurrentWeb.Site.Url + "');", true);
                }

    }

send_mail() Definition :


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

            msg.AlternateViews.Add(view);
            client = new SmtpClient(smtpServer);
            client.Send(msg);
            bool k = true;
            return k;
        }



GridViewToHtml() definition :


  private string GetGridviewData(RadGrid Grid1)
        {
            string GridRawHtml;
            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter clearWriter = new HtmlTextWriter(stringWriter);
            Grid1.RegisterWithScriptManager = false;
           
Grid1.RenderControl(clearWriter);
            GridRawHtml = clearWriter.InnerWriter.ToString();
            GridRawHtml = GridRawHtml.Remove(GridRawHtml.IndexOf("<script"), GridRawHtml.LastIndexOf("</script>") - GridRawHtml.IndexOf("<script"));
            Response.Write(GridRawHtml);
            return GridRawHtml;
        }



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.