Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
Hello,

How to send gridview in email body using c# asp.net?

I am using this code for send gridview in email body, but i have a problem for this,
One image also in my gridview, my gridview sending but image it's not sending and grid formatting also it's not sending, only simple grid sending and delete button also sending.

C#
protected void SendEmail(object sender, EventArgs e)
{
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            GridView1.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            MailMessage mm = new MailMessage("sender@gmail.com", "receiver@gmail.com");
            mm.Subject = "GridView Email";
            mm.Body = "GridView:<hr />" + sw.ToString(); ;
            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = "sender@gmail.com";
            NetworkCred.Password = "<password>";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
        }
    }
}  


My gridview it's not sending in proper format with image.
How can we send my Gridview in proper format?
Please help me.

Thanks in Advance
Ankit Agarwal
Website Developer
Posted
Updated 12-Mar-16 2:41am
v2

it may be because of your GridView contains CssClass and when HtmlTextWriter object writes HTML of GridView then it can't be able to write imported CSS, use inline CSS and before sending make delete button's column invisible...
or else create a table with in For Loop by finding controls or text of GridView...
 
Share this answer
 
Override the following method.

C#
public override void VerifyRenderingInServerForm(Control control)
    {
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900