Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wanna send an image file as html body (not as attachment) in C# Winform application. Please help.

here is the code, that I am using..

private void btn_Send_Click(object sender, EventArgs e)
       {
           Control c = zgc;
           System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(c.Width, c.Height);
           c.DrawToBitmap(bmp, c.ClientRectangle);
           bmp.Save( Application.StartupPath + "\\abc.jpg", ImageFormat.Jpeg);
           //MessageBox.Show(" Image Successfully Created...");
           SendMail();
           MessageBox.Show("Mail Sent Successfully...!","Message Sent",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);

       }

       public void SendMail()
       {
           System.Net.NetworkCredential _Credential = new System.Net.NetworkCredential(txt_UserID.Text, txt_pass.Text);
           System.Net.Mail.MailMessage _MailMessage = new MailMessage();
           _MailMessage.To.Add(txt_To.Text);
           _MailMessage.Subject = txt_subject.Text;

           _MailMessage.From = new System.Net.Mail.MailAddress(txt_To.Text);
           _MailMessage.Body = txt_Msg.Text;
           _MailMessage.IsBodyHtml = true;
           _MailMessage.Body = _MailMessage.Body;

           AlternateView av1 = AlternateView.CreateAlternateViewFromString(_MailMessage.Body, null, System.Net.Mime.MediaTypeNames.Text.Html);
           string strImageUrl = Application.StartupPath + "//abc.jpg";
           LinkedResource logo = new LinkedResource(strImageUrl, System.Net.Mime.MediaTypeNames.Image.Jpeg);
           logo.ContentId = "companylogo";
           //To refer to this image in the html body, use <img src="cid:companylogo"/>
           av1.LinkedResources.Add(logo);
           _MailMessage.AlternateViews.Add(av1);


           if (chk_Table.Checked == true)
           {
               _MailMessage.Body += htmlMessageBody(dataGridView1, dataGridView2).ToString();
           }
           if (chk_Chart.Checked == true)
           {
               System.Net.Mail.Attachment att = new Attachment(Application.StartupPath + "\\abc.jpg");
               _MailMessage.Attachments.Add(att);
           }
           //_MailMessage.Attachments.Add(new SmtpAttachment(att,"image/jpg", AttachmentLocation.Inline));






           System.Net.Mail.SmtpClient _SmtpClient = new System.Net.Mail.SmtpClient(txt_smtp.Text);
           _SmtpClient.UseDefaultCredentials = false;
           _SmtpClient.EnableSsl = Convert.ToBoolean(cb_ssl.SelectedItem.ToString());
           _SmtpClient.Credentials = _Credential;
           _SmtpClient.Port = Convert.ToInt32(txt_Port.Text);
           _SmtpClient.Send(_MailMessage);
       }

       public StringBuilder htmlMessageBody(DataGridView dg, DataGridView dg1)
       {
           StringBuilder strB = new StringBuilder();
           //create html & table
           strB.AppendLine("<html><body><center><table border='1' cellpadding='0' cellspacing='0'>");
           strB.AppendLine("</br><tr style=background-color:Silver>");
           //cteate table header
           for (int i = 0; i < dg.Columns.Count; i++)
           {
               strB.AppendLine("<td align='center' valign='middle'>" + dg.Columns[i].HeaderText + "</td>");
           }
           //create table body
           strB.AppendLine("<tr>");
           for (int i = 0; i < dg.Rows.Count - 1; i++)
           {
               strB.AppendLine("<tr>");
               foreach (DataGridViewCell dgvc in dg.Rows[i].Cells)
               {
                   strB.AppendLine("<td align='center' valign='middle'>" + dgvc.Value.ToString() + "</td>");
               }
               strB.AppendLine("</tr>");
           }
           strB.AppendLine("<tr>");
           for (int i = 0; i < 1; i++)
           {
               strB.AppendLine("<tr  style=background-color:Gray>");
               foreach (DataGridViewCell dgvc in dg1.Rows[i].Cells)
               {
                   strB.AppendLine("<td align='center' valign='middle'>" + dgvc.Value.ToString() + "</td>");
               }
               strB.AppendLine("</tr>");
           }
           //table footer & end of html file
           strB.AppendLine("</table></center><br/><br/>");

           strB.AppendLine("<center><img src=cid:companylogo\"></center></body></html>");
           return strB;
       }
Posted
Updated 25-Apr-12 1:18am
v2
Comments
Sandeep Mewara 21-Apr-12 7:53am    
Help in what? What have you tried so far?

1 solution

 
Share this answer
 
v2
Comments
djrocks0101 21-Apr-12 7:58am    
This one is not working for me..
[no name] 21-Apr-12 8:09am    
"is not working" is not very descriptive of your problem is it? How about you post the code for what you have tried and describe what is not working?
djrocks0101 25-Apr-12 7:10am    
dear I wanna send image as body, but the link given above sends the image as attachment.
[no name] 25-Apr-12 7:14am    
How about you help people out by posting the code you are trying. You just might be doing it wrong.
djrocks0101 25-Apr-12 7:18am    
I have improved my question, now it includes the code..

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