Antaris Razorengine showing images in a email template

Recently i've been using Antaris RazorEngine to do some email templating for a C# class library im working on. So far I can get general text to show on the view from a model. However when I try to show an image the engine throws and exception:

A first chance exception of type 'RazorEngine.Templating.TemplateCompilationException' occurred in RazorEngine.dll

Currently I have a UserModel class which holds 3 properties which have string data and another property which have a Bitmap image loaded from the Resources.resx file. Im wondering does this third party RazorEngine from Antaris not support rendering of images in a bitmap format ? I cant seem to find any detail documentation related to that :-(

Related Code below:

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 method within the SMTP_eMail.cs file

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/65106.html

上一篇: RazorEngine输出HTML

下一篇: Antaris Razorengine在电子邮件模板中显示图像