2016-10-26 24 views
1

我有一個ASP.Net MVC應用程序。我需要從HTML編輯器發送HTML內容。但我有問題。如何在mvc中發送動態html內容

這是我的HTML編輯器:

enter image description here

這是我的代碼:

public void MailMessageHtml(string body, string subject, string from, IEnumerable<string> to) 
    { 
     var message = new MailMessage(); 
     message.To.Add(new MailAddress("[email protected]")); 
     message.From = new MailAddress(_Settings.MailServer.UserName); 
     message.Subject = subject; 
     message.IsBodyHtml = true; 

     var htmlBody = AlternateView.CreateAlternateViewFromString(
      body, Encoding.UTF8, "text/html"); 

     message.AlternateViews.Add(
      AlternateView.CreateAlternateViewFromString(string.Empty, new ContentType("text/plain"))); 

     message.AlternateViews.Add(htmlBody); 

     using (var smtp = new SmtpClient()) 
     { 
      var credential = new NetworkCredential 
      { 
       UserName = _Settings.MailServer.UserName, 
       Password = _Settings.MailServer.Password 
      }; 
      smtp.Credentials = credential; 
      smtp.Host = _Settings.MailServer.IPAddress; 
      smtp.Port = _Settings.MailServer.Port; 
      smtp.EnableSsl = _Settings.MailServer.EnableSSL; 
      smtp.Send(message); 
     } 
    } 

但在電子郵件的身體中的HTML內容已經顯示出這樣的:

enter image description here

我該怎麼辦?

+0

這麼多的細節,但沒有解釋你的'問題'是什麼... –

+0

你可以請顯示整個功能?請致電。 – SeM

+0

這是純文本嗎?如果您在正文中指定純文本,那麼應該用於不呈現html的客戶端。 –

回答

1

您需要在將內容傳遞到主體之前解碼內容。

body = HttpUtility.HtmlDecode(body); 

就這樣。