0

So far, I have managed to embed images into my e-mail template rendered with RazorEngine. But, I am not quite satisfied with how it looks right now.
enter image description here


As it can be seen, they appear as attachments and quite in ugly way. What are the options to beautify the resulting output? Thanks in advance.

Here is the code responsible for this:
private async Task<MailMessage> PrepareEmailData(AppUser user)
{
    var (body, models) = await PrepareHtmlContentWithModels(user);

    var emailMessage = new MailMessage
    {
        From = new MailAddress("someemail@gmail.com", "Bla-bla-bla"),
        Subject = "Weekly summary",
        IsBodyHtml = true,
        Body = body,
        Priority = MailPriority.Normal,
    };

    emailMessage.To.Add(new MailAddress(user.Email));

    var alternateView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
    var imageLinks = new List<LinkedResource>();

    models.BoardThreadWithRepliesModels.ForEach(e =>
        imageLinks.Add(new LinkedResource(e.ThreadMainPicturePath, "image/png")
        {
            ContentId = e.ThreadMainPictureContentId,
            TransferEncoding = TransferEncoding.Base64
        }));

    models.BoardThreadWithRepliesModels
        .ForEach(e =>
            e.Replies
                .Where(r => !string.IsNullOrWhiteSpace(r.PicRelatedPath))
                .ToList()
                .ForEach(r =>
                {
                    imageLinks.Add(new LinkedResource(r.PicRelatedPath, "image/png")
                    {
                        ContentId = r.PicRelatedContentId,
                        TransferEncoding = TransferEncoding.Base64
                    });
                }));

    imageLinks.ForEach(e => alternateView.LinkedResources.Add(e));

    emailMessage.AlternateViews.Add(alternateView);

    return emailMessage;
}
tokechu
  • 150
  • 1
  • 14
  • You have HTML enabled on your email message, so host it somewhere and put an img tag in the body. – Mikael Dec 01 '21 at 19:48
  • The method `PrepareHtmlContentWithModels()` does exactly this, but using `cid` as `src` for `img`. Hosting for me is not an option. – tokechu Dec 01 '21 at 19:57
  • You could base64 encode the images. Then the src becomes the base64 code. https://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html – pcalkins Dec 01 '21 at 20:16
  • It's a struggle; not all mail clients support all ways, it seems. Mikael's "host it somewhere" is perhaps the most problematic one (it'd also be the best if only spammers didn't exist, as it is least bandwidth wasteful); most email clients block downloading external images. pcalkins "embed it as a data URL" does work on an increasing number of clients, but ISTR that e.g. gmail stripped them out; that may have changed. Attaching and referencing as you have done might be the widest supported one, but they often show up as attachments.. Pictures in emails are basically an all round bad idea, IMHO – Caius Jard Dec 01 '21 at 20:46
  • I seem to remember we used to have plain-text versions of all our marketing e-mails. If the client didn't support HTML they would see the plain-text version. Does that sort of thing still exist? – pcalkins Dec 02 '21 at 21:40

0 Answers0