2

I want to send the image in the body of the mail. Here, I am loading a chart based on the database records. I converted the chart into bytes by the following code:

string base64 = Request.Form[hfImageData.UniqueID].Split(',')[1];
byte[] bytes = Convert.FromBase64String(base64);

Now, I want to convert this bytes array into Image and to add the image in the body of the mail.

This is my send mail code:

 MailMessage message = new MailMessage();
 string fromEmail = "domain.com";
 string fromPW = "12345";
 string toEmail = "abcde@gmail.com";     
 message.From = new MailAddress(fromEmail);
 message.To.Add(toEmail);
 message.Subject = "Chart Mail";
 message.Body = "Hi Team";
 message.IsBodyHtml = true;
 message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
 SmtpClient smtpClient = new SmtpClient("domain.com", 1234);
 smtpClient.EnableSsl = false;
 smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
 smtpClient.UseDefaultCredentials = false;
 smtpClient.Credentials = new NetworkCredential(fromEmail, fromPW);
 smtpClient.Send(message.From.ToString(), message.To.ToString(), message.Subject, message.Body);

In the above code, I want to convert the bytes to image and need to send in the body of the mail. Please suggest.

thevan
  • 10,052
  • 53
  • 137
  • 202
  • I'm not sure how you can use an image from memory so I won't post an answer, but if the file exists on the disc you can use inline attachments quite easily. I have used this exact code myself and can confirm it works very well http://stackoverflow.com/questions/2317012/attaching-image-in-the-body-of-mail-in-c-sharp – Equalsk Aug 04 '15 at 07:51
  • Here, I dynamically load a chart based on the db records and I convert the chart into bytes. The file is not exists in the disc. – thevan Aug 04 '15 at 07:56
  • I know, I said that's why I'm not posting an answer. As there are plenty of guides on how to get an Image from a Byte array I was expecting you to do a little research yourself. Besides, as Nemmy says the constructor accepts a memorystream so you can use it directly. – Equalsk Aug 04 '15 at 08:13
  • Yes. I am doing that.. – thevan Aug 04 '15 at 08:21

1 Answers1

2

There is no need to convert the image into bytes. All you gotta do is:

string attachmentPath = Environment.CurrentDirectory + @"\test.png";
Attachment inline = new Attachment(attachmentPath);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentId = contentID;
inline.ContentType.MediaType = "image/png";
inline.ContentType.Name = Path.GetFileName(attachmentPath);

message.Attachments.Add(inline);

This will show the message in 'Inline' layout and not just as an attached image

Nemmy
  • 121
  • 7
  • Here, I dynamically load a chart based on the db records and I convert the chart into bytes. – thevan Aug 04 '15 at 07:55
  • The Attachment constructor can also get a memoryStream that you can get from your byteArray.. – Nemmy Aug 04 '15 at 08:08