Click here to Skip to main content
15,881,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to send the Gridview containing image column to the email and the image displayed in gridview from database Image is stored in database not in local folder its getting all details in mail except Image. Please Guide me to solve this problem.
my code as follows:

public void SendHTMLMail()
   {
       MailMessage Msg = new MailMessage();
       lblcomfirmationmail.Text = Session["Useremail"].ToString();
       Msg.From = new MailAddress(lblcomfirmationmail.Text);
       Msg.To.Add(lblcomfirmationmail.Text);
       Msg.Subject = "Your Order Details";
       Msg.Body += "Please check below data <br/><br/>";
       Msg.Body += GetGridviewData(Gridorderconfirmation);
       Msg.IsBodyHtml = true;
       Msg.BodyEncoding = Encoding.UTF8;
       SmtpClient smtp = new SmtpClient();
       smtp.Host = "smtp.gmail.com";
       smtp.Port = 587;
       smtp.Credentials = new System.Net.NetworkCredential("venmanirsalimited@gmail.com", "");
       smtp.EnableSsl = true;
       smtp.Send(Msg);
   }
   public string GetGridviewData(GridView gv)
   {
       StringBuilder strBuilder = new StringBuilder();
       StringWriter strWriter = new StringWriter(strBuilder);
       HtmlTextWriter htw = new HtmlTextWriter(strWriter);
       gv.RenderControl(htw);
       return strBuilder.ToString();
   }
   public override void VerifyRenderingInServerForm(Control control)
   {

   }
protected void Payimgbtn_Click(object sender, ImageClickEventArgs e)
   {
       SendHTMLMail();
       Response.Redirect("~/Payment.aspx");
   }


Image Template in Gridview
<asp:TemplateField HeaderText="Item" ItemStyle-Width="20%" ItemStyle-BorderStyle="Dotted" >
<ItemTemplate>
<asp:Image ID="image" runat="server" ImageUrl='<%# "Handler.ashx?ImID=" + Eval("ProductID")%>' Width="100" Height="100" />
</ItemTemplate>
</asp:TemplateField>

Handler.ashx
public void ProcessRequest (HttpContext context) {
        MemoryStream memoryStream = new MemoryStream();
        string imageid = context.Request.QueryString["ImID"];
        con.Open();
        SqlCommand command = new SqlCommand("select ProductImage from rsa_ProductItemTable where ProductID=" + imageid, con);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        context.Response.BinaryWrite((Byte[])dr[0]);
        con.Close();
        context.Response.End();
    }
Posted
Updated 18-Aug-15 5:16am
v2
Comments
Richard Deeming 18-Aug-15 11:32am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
F-ES Sitecore 18-Aug-15 11:38am    
Just a guess, but it's probably because the url for your image is relative to your site, ie it will just be "handler.ash?ImID=123" however when I view that in an email how does my mail client know what server to request the handler from? Ensure the url is fully qualified, ie it has the http://domain aspect also so that it will work regardless of where it is requested from.
kwelpooh 18-Aug-15 15:00pm    
Thank you so much its now showing image.
Member 11285278 12-Dec-15 1:59am    
Same Problem Here Please help me ASAP.

Thanks in Advance..
Member 11285278 23-Dec-15 4:30am    
Hey kwelpooh please help me.

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