Friday, April 20, 2012

Send GridView in Mail in asp.net,C#

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.

18 comments:

  1. Hi,
    Excellent!! work

    helped me a lot

    thank you
    vr

    ReplyDelete
  2. Hi,
    can i know what is gvPArtyStatement in this code is it a predefined method or function? what exactly this do?
    and this code is for send mail to gmail account i want to know how to send the mail to official id like internal id(abcd@hp.com).

    ReplyDelete
    Replies
    1. Here gvPArtyStatement is id of Gridview.And to send mail from internal id apply these changes to code -:

      client.Host = "74.5.21.6";//Relevent ip address
      client.EnableSsl = false;
      client.Credentials = new System.Net.NetworkCredential("abcd@hp.com", "pwd");

      Delete
  3. how to send data repeater contents to mail

    ReplyDelete
  4. Thanks a lot for this code it works great !!!

    ReplyDelete
  5. can we use this code in user control(ascx)..I Had Tried...But Showing Error "'userControl:VerifyRenderingInServerForm(System.Web.UI.Control)':no suitable method found to override"..How Can I Rectify it In User Conrol..Pls Help Me.......

    ReplyDelete
    Replies
    1. Use this method to override

      public override void VerifyRenderingInServerForm(Control control)
      {
      return;
      /* Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time. */
      }

      Delete
  6. I have requirement of sending the grid data with meeting request.
    So, I have created meeting request with ContentType as Calender and mail body with ContentType as HTML. But in the mail Not able to see the gridlines to the grid data. How I can resolve his?

    ReplyDelete
  7. I use the same code but while debugging its work however after deployment it is not working

    ReplyDelete
  8. WOW it works no need to Add anything, just change the gridID, thanks hey.

    ReplyDelete
  9. Thanks..!! it is working fyn..!!

    ReplyDelete
  10. Hi can u tell me how to send tabs in working formt in mails. i have 2 tabs in html page and that i want to send in mail but its not woorking.

    ReplyDelete
  11. Hi, i've tried sending a gridview with imagefield tag, i can send email but images r not visible in it. If you can help me in this ?

    ReplyDelete
  12. Dear , i have one column of Grid is as a image , send i send mail, it suceed but now images shown in gmail Account..pls share code for image sending , thanks

    ReplyDelete
  13. very useful
    thank you

    ReplyDelete
  14. hey can i send image containing gridview in mail.

    ReplyDelete