I’m trying to cover two common questions in one post:
- How to send an HTML message by SMTP?
- How to embed a picture into its body?
Follow the C# code below.
// Use AlternateView to create HTML body ("cid:image" - here we place the image):
using(AlternateView htmlView = AlternateView.CreateAlternateViewFromString(
"This is an <b>Html</b> e-mail message with <i>embedded image</i> below\r\n<img src='cid:image'>",
null, "text/html"))
{
// Create LinkedResource to specify an embedded image:
using (LinkedResource image = new LinkedResource("c:\\IMAGE.jpg"))
{
// ContentId should be equals to the CID that we specified above
image.ContentId = "image";
htmlView.LinkedResources.Add(image);
mail.AlternateViews.Add(htmlView);
// Create SmtpClient as reqular:
SmtpClient smtp = new SmtpClient("mail.zayko.net");
// Set Credential if needed:
smtp.Credentials = new System.Net.NetworkCredential("mailusername", "mailpassword");
// ...and here we go:
smtp.Send(mail);
}
}
d13b483d-458f-47a5-9653-8d5cf4f6a544|0|.0