从视图接收html字符串到替换为模板

时间:2018-05-17 02:41:33

标签: c# asp.net-mvc

我有一个控制器,我执行电子邮件服务,如:

var englishtemplatePath = "HtmlTemplate/EnglishTemplate.html";
                    var englishTemplate = Path.Combine(_hostingEnvironment.WebRootPath, englishtemplatePath);
                    var englishFileContent = System.IO.File.ReadAllText(englishTemplate);
                    var englishEmailTemplate = new EmailTemplateModel
var englishEmailTemplate = new EmailTemplateModel
                    {
                        CallBackUrl = callbackurl,
                        UrlText = "text1",
                        WelcomeText = "text2",
                        VerifyText = "text3",
                        VerifyNowText = "ACTIVATE",
                        TroubleText = "text4",
                        Footer = "text5"
                    };

然后我有一个服务来发送这样的电子邮件:

public async Task Execute(string email, string subject, EmailTemplateModel model, string template)
        {
            try
            {
                string toEmail = string.IsNullOrEmpty(email)
                                 ? _emailSettings.ToEmail
                                 : email;
                MailMessage mail = new MailMessage()
                {
                    From = new MailAddress(_emailSettings.UsernameEmail, "Subject")
                };
                mail.To.Add(new MailAddress(toEmail));
                mail.CC.Add(new MailAddress(_emailSettings.CcEmail));


                mail.Subject =  subject;

                mail.Body = template.Replace("{{CallBackUrl}}", model.CallBackUrl)
                                    .Replace("{{UrlText}}", model.UrlText)
                                    .Replace("{{WelcomeText}}", model.WelcomeText)
                                    .Replace("{{VerifyText}}", model.VerifyText)
                                    .Replace("{{VerifyNowText}}", model.VerifyNowText)
                                    .Replace("{{TroubleText}}", model.TroubleText)
                                    .Replace("{{CallBackUrl}}", model.CallBackUrl)
                                    .Replace("{{Footer}}", model.Footer);

                mail.IsBodyHtml = true;
                mail.Priority = MailPriority.High;

                using (SmtpClient smtp = new SmtpClient(_emailSettings.PrimaryDomain, _emailSettings.PrimaryPort))
                {
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new NetworkCredential(_emailSettings.UsernameEmail, _emailSettings.UsernamePassword);
                    smtp.EnableSsl = true;
                    await smtp.SendMailAsync(mail);
                }
            }
            catch (Exception ex)
            {
                //do something here
            }
        }

如您所见,我从模型中接收参数,并将其替换为参数如下的模板:

{{CallBackUrl}}

它运作良好,但现在我想要接收html表,如:

<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th> 
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
  <tr>
    <td>John</td>
    <td>Doe</td>
    <td>80</td>
  </tr>
</table>

我怎样才能收到它?在简单的字符串?我将其替换为其他人?只是使用替换?此致

1 个答案:

答案 0 :(得分:0)

你提到的电子邮件会显示原始HTML吗? 如果是,您可以用&lt; table&gt;替换标签表td tr。 &LT; TD&GT; .. 参考:What do &lt; and &gt; stand for?