Insert images in email template

237 views Asked by At

I'm building a method that will send an email with some images. My template is a partial view based on a collection of its view model. The images are base64 strings that I put in the src of the img tag (inline images), and when the partial view is fully built, I parse the view to a string with RazorEngine, because the function I consume to send the email (with NotificationHubClient) will send the html string as a parameter of a SendGrid template.

However when I receive the email, the images aren't shown in any client, I guess the email clients blocks the inline images. So I think on use CIDs to attatch the images. I have used that method sometime ago, but to make the CIDs works properly I have to generate an AlternateView, but AlternateView only works with MailMessage (System.Net.Mail).

I found this thread: How to convert EmailMessage alternate views into SendGrid Html and Text And I tried to build something similar:

public static string GenerateHTML(AlternateView alternateView, string template)
    {
        MailMessage messageTemp = new MailMessage{ Body = template, IsBodyHtml = true };
        messageTemp.AlternateViews.Add(alternateView);
        var stream = messageTemp.AlternateViews[0].ContentStream;
        using(var rd = new StreamReader(stream))
        {
            return rd.ReadToEnd();
        }
    }

I pass the returned string as a parameter to the Send function, but when I receive the mail, again the images aren't shown, and even my css is gone.

Do you know a way to convert a MailMessage with its AlternateView to a html string with the attatched images?

If you need them, these are the functions to send the email and to generate the AlternateView.

public static void SendNotification(List<string> recipients, Dictionary<string, string> customParams, string template)
    {
        string hubURL = ConfigurationManager.AppSettings["NotificationHubUrl"];
        string apiKey = ConfigurationManager.AppSettings["NotificationHubApiKey"];
        NotificationHubClient.NotificationHubClient client = new NotificationHubClient.NotificationHubClient(hubURL, apiKey);
        Dictionary<string, string> authParams = new Dictionary<string, string>();
        authParams["username"] = ConfigurationManager.AppSettings["SendGridUser"];
        authParams["password"] = ConfigurationManager.AppSettings["SendGridPassword"];
        NotificationHubModels.NotificationResponse response = client.SendNotification(new NotificationHubModels.NotificationMessage()
        {
            Type = "EMAIL",
            Provider = "SENDGRID",
            Template = template,
            CustomParams = customParams,
            Recipients = recipients,
            ProviderAuthParams = authParams,
        });
    }

public static AlternateView InsertImages(AlternateView alternateView, IList<Tuple<MemoryStream, string>> images)
    {
        foreach(var item in images)
        {
            LinkedResource linkedResource = new LinkedResource(item.Item1, "image/jpg");
            linkedResource.ContentId = item.Item2;
            linkedResource.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

            alternateView.LinkedResources.Add(linkedResource);
        }

        return alternateView;
    }

Thanks in advance.

0

There are 0 answers