Send Scheduled Email using email Templates in .NET MVC3 using C#

1.4k views Asked by At

I am working on a .NET MVC3 Web Application In this my goal is to send Scheduled E-mail in which I will be using E-mail Templates. I am so confused what should I follow to acquire my goal.

I tried MVC Mailer. But It doesn't works with Scheduler.(Fluent Scheduler) I tried using RazorEngine with Email templates but somehow not succeeded in attaching HTML Email Templates.

Please Help...

1

There are 1 answers

5
Daniel Conde Marin On

Using RazorEngine something like this should help, is very straightforward:

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

Hope it helps

EDIT:

Suppose you have a template called "TestTemplate":

Dear @Model.Name

Just imagine like if this were a normal cshtml view and put your model properties like this: @Model.SomeProperty

Cheers.

With the previous template located in the path I prefixed in my helper method AttempLoadEmailTemplate, then you can send an email like this:

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

mailService.SendEmailMessage("TestTemplate", viewModel, "[email protected]", "[email protected]", "testing razor engine", null);