0

いくつかの画像を含むメールを送信するメソッドを構築しています。私のテンプレートは、そのビュー モデルのコレクションに基づく部分ビューです。画像は、img タグ (インライン画像) の src に入れた base64 文字列です。部分ビューが完全に構築されたら、RazorEngine を使用してビューを文字列に解析します。 NotificationHubClient) は HTML 文字列を SendGrid テンプレートのパラメーターとして送信します。

ただし、電子メールを受信すると、どのクライアントにも画像が表示されません。電子メール クライアントがインライン画像をブロックしていると思われます。したがって、CIDを使用して画像を添付すると思います。しばらく前にその方法を使用しましたが、CID を適切に機能させるには、AlternateView を生成する必要がありますが、AlternateView は MailMessage (System.Net.Mail) でのみ機能します。

私はこのスレッドを見つけました: How to convert EmailMessage alternative views into SendGrid Html and Text そして、似たようなものを構築しようとしました:

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

返された文字列をパラメーターとして Send 関数に渡しますが、メールを受信すると、やはり画像が表示されず、CSS も失われています。

AlternateView を含む MailMessage を、添付された画像を含む html 文字列に変換する方法を知っていますか?

必要な場合、これらは電子メールを送信し、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;
    }

前もって感謝します。

4

0 に答える 0