在MVC3中使用Razor模板发送电子邮件

3

有人能帮助我做出决定吗? 我在网上看到了一些变体,但我不想使用其他人已经做好的东西。 我想利用 .net 提供的功能来创建自己的功能。

任何帮助都将不胜感激。提前谢谢你。


我是通过这个链接里的信息来完成的:http://justincase.kodingen.com/articles/dev/frameworks/net-dev/razor/send-emails-with-dynamic-content-using-razor - Diana Dimitrova
http://justincase.kodingen.com/ 看起来已经不存在了。 - Al Lelopath
2个回答

4

这件事真的很简单:

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

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接