使用MailItem.HTMLBody属性通过MVC控制器发送电子邮件

时间:2019-07-04 09:16:46

标签: c# html css email model-view-controller

我正在尝试使用MailItem.HTMLBody属性通过MVC控制器发送电子邮件,该属性易于发送,但是当我尝试添加一些html属性和类时,它不适用于电子邮件正文内容,我想在我的电子邮件中发送身体内容   [图片:https://i.stack.imgur.com/IAkMU.jpg

  

控制器代码

var GetProgramM = corporate.FindProgramM(GetTeam.teamid, "Program Management");
if (GetProgramM.email != null)
{
   brief.EmailsentprogramManagemnet = 1;
   briefRepo.AddRecord(brief);
   briefRepo.savechanges();
   string from = ConfigurationManager.AppSettings["SMTPuser"];
   string host = ConfigurationManager.AppSettings["Host"];
   int port = int.Parse(ConfigurationManager.AppSettings["EmailPort"]);
   bool enableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSSL"]);
   string user = ConfigurationManager.AppSettings["SMTPuser"];
   string password = ConfigurationManager.AppSettings["SMTPpassword"];
   string email = GetProgramM.email;
   var mailmessage = new MimeMessage();
   mailmessage.From.Add(new MailboxAddress("SHARP", from));
   mailmessage.To.Add(new MailboxAddress("Not Reply", email));
   mailmessage.Subject = "Request For Document Approval";

   var body = new BodyBuilder();
   var project1 = projectrepo.GetAll().Where(x => x.NewProject_Id == brief.NewProject_Id).FirstOrDefault();
   string projectname = project1.Project_Title;
   string author = brief.author;
   string link = Url.Action("ProgramMapprovalView", "ProjectBrief", new System.Web.Routing.RouteValueDictionary(new { id = brief.ProjectBriedId }), "http", Request.Url.Host);

   //// see plus image 
   var pathImage = Path.Combine(Server.MapPath("~/content/seepulse.png"));
   var image = body.LinkedResources.Add(pathImage);
   image.ContentId = MimeUtils.GenerateMessageId();
   var pathImage1 = Path.Combine(Server.MapPath("~/content/mandate-email.jpg"));
   var image1 = body.LinkedResources.Add(pathImage1);
   image1.ContentId = MimeUtils.GenerateMessageId();

   body.HtmlBody = string.Format(@"<center><img src=""cid:{0}"" alt=""Invite""><b><h4>{1} requested for approval of Project Brief document</h4></b><br>Your Project:<b>{2}</b><br>Starting Up Stage:<b>{3}</b><br><br><a href= ""{4}"" > <img src=""cid:{5}"" alt=""Invite""> </a></center>", image1.ContentId, author, projectname, "Starting Up Project", link, image.ContentId);

   mailmessage.Body = body.ToMessageBody();
   var smtp = new SmtpClient();
   smtp.Connect(host, port);
   smtp.AuthenticationMechanisms.Remove("XOAUTH2");
   smtp.Authenticate(user, password);
   smtp.Send(mailmessage);
   smtp.Disconnect(true);
}

0 个答案:

没有答案