2011-03-30 42 views
7

我想使用Razor视图引擎生成电子邮件。在Web项目之外使用Razor

从我读过,我可以使用这些操作系统做的项目,从我的网站:

然而,所有这些使用用户提交表单并从控制器发送电子邮件的示例。在创建电子邮件时,ControllerContext似乎是必需的。我希望定期在我的“服务”层中进行生成,所以我怀疑我有权访问ControllerContext。

这是可行的吗?

回答

8

检查this了。我使用它作为模板,像这样的嵌入的资源(但可以进行修改,以在任何地方找到模板):

public static class MailSender 
{ 
    static MailSender() 
    { 
     CompilerServiceFactory = new DefaultCompilerServiceFactory(); 
     TemplateService = new TemplateService(CompilerServiceFactory.CreateCompilerService(), typeof(MailTemplate<>)); 
    } 

    private static ICompilerServiceFactory CompilerServiceFactory { get; set; } 
    private static TemplateService TemplateService { get; set; } 

    private static ITemplate<T> GetTemplate<T>(T model) 
    { 
     string path = typeof(T).FullName; 

     var assembly = Assembly.GetExecutingAssembly(); 

     using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path)) 
     { 
      if (stream == null) 
       throw new FileNotFoundException("Mail template not found"); 

      using (var reader = new StreamReader(stream)) 
      { 
       string source = reader.ReadToEnd(); 

       return TemplateService.GetTemplate<T>(source, model); 
      } 
     } 
    } 

    public static void Send<T>(string to, T model) 
    { 
     Send(new string[] { to }, new string[] { }, new string[] { }, model); 
    } 
    public static void Send<T>(string to, string cc, string bcc, T model) 
    { 
     Send(new string[] { to }, new string[] { cc }, new string[] { bcc }, model); 
    } 
    public static void Send<T>(string[] to, string[] cc, string[] bcc, T model) 
    { 
     var template = (MailTemplate<T>)GetTemplate<T>(model); 

     template.Execute(); 

     Trace.WriteLine(string.Format("To: {0} Subject: {1}{3}Body: {2}", string.Join(",", to), template.Subject, template.Result, Environment.NewLine), "Mail"); 
#if !LOCAL 
     using (var message = new MailMessage()) 
     using (var client = new SmtpClient()) 
     { 
      if (!string.IsNullOrWhiteSpace(template.From)) 
       message.From = new MailAddress(template.From); 

      message.To.Add(string.Join(",", to)); 

      if (cc != null && cc.Length > 0) 
       message.CC.Add(string.Join(",", cc)); 

      if (bcc != null && bcc.Length > 0) 
       message.Bcc.Add(string.Join(",", bcc)); 

      message.Subject = template.Subject; 

      message.Body = template.Result; 
      message.IsBodyHtml = true; 

      client.Send(message); 
     } 
#endif 
    } 
} 

public abstract class MailTemplate<TModel> : TemplateBase<TModel> 
{ 
    public string From { get; set; } 
    public string Subject { get; set; } 
} 

型号:

public class Contact 
{ 
    public string Name { get; set; } 
    public string Email { get; set; } 
    public string Text { get; set; } 
} 

模板:

@inherits Feedback.Lib.Mail.MailTemplate<Feedback.Lib.Mail.Templates.Contact> 
@{ 
    From = string.Format("{0} <{1}>", Model.Name, Model.Email); 
    Subject = "Contact request - " + From; 
} 

@Model.Text 

我有对于每个模板都是自己的模型,所以很容易通过类名来定位它们。此外,我无法进行智能感知工作。

+0

嘿!我不能在这里拉我自己的项目?! :) – Buildstarted 2011-03-30 15:36:56

0

,所以我用它来获得外界的asp.net剃须刀什么是CodePlex上找到的剃刀引擎: RazorEngine