Возможно, вам следует попробовать использовать AlternateView.Вы должны назначить Id для ресурса и использовать этот идентификатор в теге HTML <img>
.Атрибут src
должен адресовать этот Id.Like так:
<img src="cid:ResourceId" />
Не забудьте добавить связанный ресурс в альтернативное представление.
Вот полный код, который я использовал:
Byte[] iconBytes = Convert.FromBase64String(@"iVBOR IMAGE BYTES Hy4vAAN==");
System.IO.MemoryStream iconBitmap = new System.IO.MemoryStream(iconBytes);
LinkedResource iconResource = new LinkedResource(iconBitmap, MediaTypeNames.Image.Jpeg);
iconResource.ContentId = "Icon";
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.To.Add(new MailAddress("recipient@domain.com", "Recipient Name"));
msg.From = new MailAddress("sender@domain.com", "Sender Name");
msg.Subject = "Attach image to mail";
string htmlBody = @"<html><head>";
htmlBody += @"<style>";
htmlBody += @"body{ font-family:'Calibri',sans-serif; }";
htmlBody += @"</style>";
htmlBody += @"</head><body>";
htmlBody += @"<h1>The attached image is here below</h1>";
htmlBody += @"<img src='cid:" + iconResource.ContentId + @"'/>";
htmlBody += @"</body></html>";
AlternateView alternativeView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
alternativeView.LinkedResources.Add(iconResource);
msg.AlternateViews.Add(alternativeView);
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Port = 25; // You can use Port 25 if 587 is blocked
client.Host = "smtp.yourhost.com";
client.Send(msg);