Embedded Email Image Does Not Show Up

I don't know why the image does not displayed.
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);
However, if i use Base64, it does displayed on Outlook, but not in Gmail. Why was that?
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>;
No description
2 Replies
แผ”ฯฯ‰ฯ‚
this is the problem in the first one: <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="
the image doesn't show up because you didn't send anything in the src
nar
narOPโ€ข4d ago
Sorry for late reply. The issue is turn out the library used cannot attach image

Did you find this page helpful?