Diana Dimitrova
Diana Dimitrova

Reputation: 135

Send emails in MVC3 using razor templates

Can anyone help me to take a decision? I see some variants in the net but i don't want to have to use some ready stuff from other guys did. I want to create my own functionality using what .net gives as possibilities.

Any help will be appreciated. Thank u in advance

Upvotes: 3

Views: 4287

Answers (2)

Diana Dimitrova
Diana Dimitrova

Reputation: 135

It was really simple to do it:

            var emailModelData = new
            {
                Name = "John Someone",

                OrganizationName = "OragnizationName"

            };

            var templatePath = physicalApplicationPath +
                @"EmailTemplates\" + Mail.Default.WelcomeMailTemplate + ".cshtml";

            var template = System.IO.File.ReadAllText(templatePath, Encoding.Default);
            var body = Razor.Parse(template, emailModelData);

            //this is doesnt matter it is just and object which i pass to the SendEmail function below
            var simpleMailMessage = new SimpleMailMessage
            {
                To = recipientEmail,
                From = Mail.Default.From,
                Body = body,
                IsBodyHtml = true,
                ReplyTo = Mail.Default.ReplyTo,
                Subject = "subject blah blah"
            };
            //If using a test mail address. Used in development
            if (Mail.Default.SendToTestEmailAddress)
            {

                simpleMailMessage.Bcc = string.Empty;
                simpleMailMessage.CC = string.Empty;
                simpleMailMessage.To = Mail.Default.TestEmailAddress;
            }
//see below the code for the SendEmail funciton
            _email.SendEmail(simpleMailMessage, null, Constants.WEBSITE_SOURCE_NAME);

//--------------------------------------------------------

public bool SendMail(SimpleMailMessage mail)
        {


            using (var client = new SmtpClient()) 
            {
                try
                {
                    var email = CreateMailMessage(mail);
                    client.Send(email);
                    return true;

                }
                catch (Exception exception)
                {
                     return false;

                }

            }
            return false;

        }

        private static System.Net.Mail.MailMessage CreateMailMessage(SimpleMailMessage mail)
        {
            var msg = new System.Net.Mail.MailMessage();

            if (!string.IsNullOrEmpty(mail.From))
                msg.From = new MailAddress(mail.From);

            if (!string.IsNullOrEmpty(mail.Subject))
                msg.Subject = mail.Subject;

            if (!string.IsNullOrEmpty(mail.Body))
                msg.Body = mail.Body;

            msg.IsBodyHtml = true;

            if (!string.IsNullOrEmpty(mail.Sender))
                msg.Sender = new MailAddress(mail.Sender);

            if (!string.IsNullOrEmpty(mail.To))
                msg.To.Add(new MailAddress(mail.To));

            if (!string.IsNullOrEmpty(mail.ReplyTo))
                msg.ReplyToList.Add(new MailAddress(mail.ReplyTo));

            if (!string.IsNullOrEmpty(mail.CC))
                msg.CC.Add(new MailAddress(mail.CC));

            if (!string.IsNullOrEmpty(mail.Bcc))
                msg.Bcc.Add(mail.Bcc);

            return msg;
        }

Upvotes: 4

John x
John x

Reputation: 4031

here are few links which may help

Postal

and here is the related blog post http://aboutcode.net/2010/11/17/going-postal-generating-email-with-aspnet-mvc-view-engines.html

Upvotes: 0

Related Questions