asp.net mvc-如何在mvc中添加自己的模板视图收件箱



我已经创建了MVC5项目。现在使用邮件发送概念。它运行良好。发送电子邮件效果很好。现在我计划在我的电子邮件中添加一些设计。我在App_Start/template/EmailTEmplate.html 中创建了以下模板

<html>
<body style="color:grey; font-size:15px;">
    <font face="Helvetica, Arial, sans-serif">
        <div style="position:absolute; height:100px;
width:600px; background-color:0d1d36; padding:30px;">
            <img src="logo" />
        </div>
        <br />
        <br />
        <div style="background-color: #ece8d4;
width:600px; height:200px; padding:30px; margin-top:30px;">
            <p>Dear {0},<p>
            <p>Thank you</p>
        </div>
</body>
</html>

这里我的控制器代码邮件发送到收件箱

public class ContactUsController : Controller
    {
        // GET: Contact
        public ActionResult ContactUs()
        {
            return View();
        }


   [HttpPost]
    public ActionResult ContactUs(MailModel objModelMail, HttpPostedFileBase fileUploader)
    {
        if (ModelState.IsValid)
        {
            string from = "xxx@gmail.com"; 
            using (MailMessage mail = new MailMessage(from, objModelMail.To))
            {
                mail.Subject = objModelMail.Subject;
                mail.Body = objModelMail.Body;
                if (fileUploader != null)
                {
                    string fileName = Path.GetFileName(fileUploader.FileName);
                    mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
                }
                mail.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                NetworkCredential networkCredential = new NetworkCredential(from, "password");
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = networkCredential;
                smtp.Port = 587;
                smtp.Send(mail);
                ViewBag.Message = "Sent";
                return View("ContactUs", objModelMail);
            }
        }
        else
        {
            return View();
        }
    }
}

我想让收件箱里的邮件看起来好看吗?如何添加模板?

您可以在视图页面中分配布局:

@{
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}

是你的问题吗?

最新更新