Embedded Email Image Does Not Show Up
I don't know why the image does not displayed.
However, if i use Base64, it does displayed on Outlook, but not in Gmail. Why was that?
string wwwRootPath = _webHostEnvironment.WebRootPath;
string imagePath = Path.Combine(wwwRootPath, "images", "HappyBirthdayCard.jpeg");
if (!File.Exists(imagePath))
{
throw new FileNotFoundException($"File not found at {imagePath}");
}
LinkedResource imageResource = new(imagePath, MediaTypeNames.Image.Jpeg)
{
ContentId = "birthdayImage",
};
string htmlBody = $@"
<p>Dear aa,</p>
<p>Happy Birthday, aa ๐ We send you our warmest wishes on this special day.</p>
<p>Wishing you a very Happy Birthday and all the best on your special day. Enjoy this day to the fullest โจ</p>
<p><img src=""cid:birthdayImage"" alt=""Happy Birthday!"" width=""300""/></p>
<p>Best Regards,</p>
<p><strong>ABC</strong>;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
htmlView.LinkedResources.Add(imageResource);
var mail = new MailMessage
{
Subject = $"Happy Birthday aa! ๐",
IsBodyHtml = true,
Body = htmlBody,
};
mail.AlternateViews.Add(htmlView);
mail.To.Add("aaaaa");
await mailService.SendMailAsync(mail);
string wwwRootPath = _webHostEnvironment.WebRootPath;
string imagePath = Path.Combine(wwwRootPath, "images", "HappyBirthdayCard.jpeg");
if (!File.Exists(imagePath))
{
throw new FileNotFoundException($"File not found at {imagePath}");
}
LinkedResource imageResource = new(imagePath, MediaTypeNames.Image.Jpeg)
{
ContentId = "birthdayImage",
};
string htmlBody = $@"
<p>Dear aa,</p>
<p>Happy Birthday, aa ๐ We send you our warmest wishes on this special day.</p>
<p>Wishing you a very Happy Birthday and all the best on your special day. Enjoy this day to the fullest โจ</p>
<p><img src=""cid:birthdayImage"" alt=""Happy Birthday!"" width=""300""/></p>
<p>Best Regards,</p>
<p><strong>ABC</strong>;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
htmlView.LinkedResources.Add(imageResource);
var mail = new MailMessage
{
Subject = $"Happy Birthday aa! ๐",
IsBodyHtml = true,
Body = htmlBody,
};
mail.AlternateViews.Add(htmlView);
mail.To.Add("aaaaa");
await mailService.SendMailAsync(mail);
byte[] imageBytes = File.ReadAllBytes(imagePath);
string base64Image = Convert.ToBase64String(imageBytes);
string htmlBody = $@"
<p>Dear aa,</p>
<p>Happy Birthday, aa ๐ We send you our warmest wishes on this special day.</p>
<p>Wishing you a very Happy Birthday and all the best on your special day. Enjoy this day to the fullest โจ</p>
<p><img src='data:image/jpeg;base64,{base64Image}' alt='Happy Birthday!' width='300'/></p>
<p>Best Regards,</p>;
byte[] imageBytes = File.ReadAllBytes(imagePath);
string base64Image = Convert.ToBase64String(imageBytes);
string htmlBody = $@"
<p>Dear aa,</p>
<p>Happy Birthday, aa ๐ We send you our warmest wishes on this special day.</p>
<p>Wishing you a very Happy Birthday and all the best on your special day. Enjoy this day to the fullest โจ</p>
<p><img src='data:image/jpeg;base64,{base64Image}' alt='Happy Birthday!' width='300'/></p>
<p>Best Regards,</p>;

2 Replies
this is the problem in the first one:
the image doesn't show up because you didn't send anything in the
<img src=""cid:birthdayImage"" alt=""Happy Birthday!"" width=""300""/>
you see the first doubled double quotes?
it's closing the first string:
c#
$@"
<p>Dear aa,</p>
<p>Happy Birthday, aa ๐ We send you our warmest wishes on this special day.</p>
<p>Wishing you a very Happy Birthday and all the best on your special day. Enjoy this day to the fullest โจ</p>
<p><img src="
c#
$@"
<p>Dear aa,</p>
<p>Happy Birthday, aa ๐ We send you our warmest wishes on this special day.</p>
<p>Wishing you a very Happy Birthday and all the best on your special day. Enjoy this day to the fullest โจ</p>
<p><img src="
src
Sorry for late reply. The issue is turn out the library used cannot attach image