Kevin Powell - CommunityKP-C
Kevin Powell - Communityโ€ข11mo agoโ€ข
5 replies
nar

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);


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>;
Screenshot_2025-03-10_115036.png
Was this page helpful?