Antaris Razorengine在电子邮件模板中显示图像

最近我一直在使用Antaris RazorEngine来为一个C#类库做一些电子邮件模板工作。 到目前为止,我可以获得一般文本以显示模型的视图。 但是,当我尝试显示引擎引发的图像和异常时:

RazorEngine.dll中发生类型'RazorEngine.Templating.TemplateCompilationException'的第一次机会异常

目前我有一个UserModel类,其中包含3个属性,其中包含字符串数据,另一个属性具有从Resources.resx文件加载的位图图像。 林不知道这个来自Antaris的第三方RazorEngine是否不支持以位图格式呈现图像? 我似乎无法找到任何与此相关的详细文档:-(

相关代码如下:

Email_Template.cshtml

@Model MVC_Util_Naqi.UserModel

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <img src="@Model.LogoImage" alt="" width="500" height="250" />
    </head>
    <body>
        <title>Title Goes here</title>
        <p> Hi @Model.Name,</p>
        <p>Thanks for signing up to <a href="http://awesomesite.com">our awesome site</a>!</p>
        @if (!Model.IsPremiumUser) {
            <p>To make the most of it, <a href="http://awesomesite.com/upgrade">upgrade to our premium package now</a>!</p>
        }
        <p>John, Founder</p>
    </body>
</html>

SMTP_eMail.cs文件中的SMTP电子邮件方法

public void ReadyTheEmail(){
    this.SMTP_HostName = "---------";
    this.SMTP_HostPort = -----;
    this.SMTP_Username = "-----";
    this.SMTP_Password = "-------";
    this.eMailFromAddress = "---------";
    this.eMailToAddress = "---------------";
    this.eMailSubject = "Test - C# Email Functionality";
    this.eMailHTMLBody = "";
    this.IsSSLEnabled = true;
    try{
        if (TestConnection(SMTP_HostName, SMTP_HostPort)){
            smtpServer.Host = SMTP_HostName;
            smtpServer.Credentials = new System.Net.NetworkCredential(SMTP_Username, SMTP_Password);
            smtpServer.EnableSsl = IsSSLEnabled;
            mail.From = new MailAddress(eMailFromAddress);
            mail.To.Add(eMailToAddress);
            mail.Subject = eMailSubject;
            mail.IsBodyHtml = true;
            eMailHTMLBody = "<h1> HTML Body goes here :-) </h1>";
            StringBuilder sb = new StringBuilder(eMailHTMLBody);
            System.Reflection.Assembly myAssembly;
            myAssembly = this.GetType().Assembly;
            System.Resources.ResourceManager myRManager = new System.Resources.ResourceManager("MVC_Util_Naqi.Properties.Resources", myAssembly);
            byte[] file = (byte[])myRManager.GetObject("Email_Template");
            MemoryStream ms = new MemoryStream(file);
            StreamReader sReader = new StreamReader(ms);
            string template = sReader.ReadToEnd();
            //string template_final = RemoveInheritsDirective(template);
            UserModel model = new UserModel() { Name = "Ali", Email = "Ali@gmail.com", IsPremiumUser = true };
            string template1 = @"@Model MVC_Util_Naqi.UserModel
                               Hello @Model.Name, welcome to RazorEngine!
                               Hello @Model.Email, welcome to RazorEngine!
                               Hello @Model.IsPremiumUser, welcome to RazorEngine!";

            //var result = Razor.Parse(template, model, "template1");
            Razor.Compile(template, typeof(UserModel), "compiled-tempalte");
            string result = Razor.Run(model, "compiled-tempalte");
            sb.Append(result);
            //ResourceSet resourceSet = Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
            //byte[] file = (byte[]) resourceSet.GetObject("PDF_eMail.cshmtl");
            //MemoryStream ms = new MemoryStream(file);
            //foreach (DictionaryEntry entry in resourceSet) {
                //System.Diagnostics.Debug.WriteLine(entry.Key.ToString());
                //object resource = entry.Value;
                //System.Diagnostics.Debug.WriteLine(entry.Value.GetType());
            //}
            mail.Body = sb.ToString();
        }
        else{
            System.Diagnostics.Debug.WriteLine("Connection Problem");
        }
    }
    catch (Exception ex){
        Console.WriteLine(ex.ToString());
    }
}
链接地址: http://www.djcxy.com/p/65105.html

上一篇: Antaris Razorengine showing images in a email template

下一篇: Create a URL in a template using RazorEngine