0

I have to send a GridView containing an image column by email. The image displayed in the GridView is from a database so the image is stored there and not in a local folder.

Here's my code:

  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");
   }

I'm able to send all the details in a mail except the Image. How can I get this working?

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
mini
  • 11
  • 2
  • If I were you, I wouldn't use a GridView at all for this. Instead, I'd create the HTML using [Razor](https://github.com/Antaris/RazorEngine). For the images, you could try retrieving the image bytes from the database, then adding them as a base 64 encoded string to the `` element. That is, assuming you test to make sure email clients support base64 images. Which I'm not sure they do. – mason Aug 18 '15 at 16:01
  • You're looking for `inline attachments`. There's a good demo on [this SO post](http://stackoverflow.com/questions/1212838/c-sharp-sending-mails-with-images-inline-using-smtpclient). If I remember correctly the constructor also accepts a MemoryStream for the image so you can just pass it the byte array straight out of the database, but I am fuzzy on the details. – Equalsk Aug 18 '15 at 16:17
  • Do you have an absolute path to the image location (web url) instead of a relative in your email? – Ricketts Aug 18 '15 at 16:19

1 Answers1

0

When you get images from your file, For example, your method is: "~/Images/"+. instead of "~" use your current web address. Example, "yourdomain.com/Images/". It works for me.

Ajim
  • 1