使用C#在.NET MVC3中使用电子邮件模板发送预定电子邮件

时间:2013-12-11 13:34:16

标签: c# asp.net-mvc-3 scheduled-tasks email-templates

我正在开发 .NET MVC3 Web应用程序 在此我的目标是发送预定的电子邮件,我将在其中使用电子邮件模板。 我很困惑应该遵循什么来获得我的目标。

我试过了MVC Mailer。但它不适用于Scheduler。(Fluent Scheduler) 我尝试将RazorEngine与电子邮件模板结合使用,但不知何故未能成功附加HTML Email Templates

请帮助......

1 个答案:

答案 0 :(得分:1)

使用RazorEngine之类的东西应该会有所帮助,这非常简单:

public bool SendEmailMessage(string template, object viewModel, string to, string @from, string subject, params string[] replyToAddresses)
    {
       var compiledTemplate = LoadTemplate(template, viewModel);

       return  SendEmail(from, to, subject, compiledTemplate, from, null, replyToAddresses);

    }

public bool SendEmailMessageWithAttachments(string template, object viewModel, string to, string @from, string subject, List<Attachment> attachedFiles, params string[] replyToAddresses)
        {
            var compiledTemplate = LoadTemplate(template, viewModel);
            return SendEmail(from, to, subject, compiledTemplate, from, attachedFiles, replyToAddresses);
        } 

 public string LoadTemplate(string template, object viewModel)
        {
            var templateContent = AttemptLoadEmailTemplate(template);
            var compiledTemplate = Razor.Parse(templateContent, viewModel);

            return compiledTemplate;
        }

    private string AttemptLoadEmailTemplate(string name)
    {
        if (File.Exists(name))
        {
            var templateText = File.ReadAllText(name);
            return templateText;
        }

        var templateName = string.Format("~/Data/EmailTemplates/{0}.html", name); //Just put your path to a scpecific template
        var emailTemplate = HttpContext.Current.Server.MapPath(templateName);

        if (File.Exists(emailTemplate))
        {
            var templateText = File.ReadAllText(emailTemplate);
            return templateText;
        }

        return null;
    }

    private bool SendEmail(string from, string to, string subject, string body, string replyTo, List<Attachment> attachedFiles, params string[] replyToAddresses)
            {
                replyTo = replyTo ?? from;
                attachedFiles = attachedFiles ?? new List<Attachment>();

                var message = new MailMessage(from, to, subject, body);
                message.ReplyToList.Add(replyTo);

                foreach (var attachedFile in attachedFiles)
                    message.Attachments.Add(attachedFile);

        try
        {
            smtpClient.SendAsync(email, null);
            return true;
        }
        catch (Exception exption)
        {
            return false;
        }
      }

希望有所帮助

修改

假设您有一个名为“TestTemplate”的模板:

亲爱的@ Model.Name

想象一下,如果这是一个普通的cshtml视图,并将您的模型属性设置为:@ Model.SomeProperty

干杯。

将前一个模板放在我的帮助方法AttempLoadEmailTemplate前缀的路径中,然后您可以发送这样的电子邮件:

var viewModel = new { Name = "Aks", SomeProperty = "Foo" };

mailService.SendEmailMessage("TestTemplate", viewModel, "aks@gmail.com", "daniel@gmail.com", "testing razor engine", null);