2011-12-25 66 views
97

Tôi có thể cho phép ứng dụng web gửi email tự động bằng Windows Task Scheduler. Bây giờ tôi muốn gửi email định dạng HTML bằng cách sử dụng phương pháp sau mà tôi đã viết để gửi email.Làm cách nào để gửi email được định dạng HTML?

code-behind của tôi:

protected void Page_Load(object sender, EventArgs e) 
    { 
     SmtpClient sc = new SmtpClient("mail address"); 
     MailMessage msg = null; 

     try 
     { 
      msg = new MailMessage("[email protected]", 
       "[email protected]", "Message from PSSP System", 
       "This email sent by the PSSP system"); 

      sc.Send(msg); 
     } 

     catch (Exception ex) 
     { 
      throw ex; 
     } 

     finally 
     { 
      if (msg != null) 
      { 
       msg.Dispose(); 
      } 
     } 
    } 

Làm thế nào để làm điều đó? Tôi chỉ muốn đặt một số văn bản in đậm với một liên kết và có thể một hình ảnh trong email.

Trả lời

158

Thiết isBodyHtml để true cho phép bạn sử dụng các thẻ HTML trong nội dung thư:

msg = new MailMessage("[email protected]", 
       "[email protected]", "Message from PSSP System", 
       "This email sent by the PSSP system<br />" + 
       "<b>this is bold text!</b>"); 

msg.IsBodyHtml = true; 
+0

Chúng tôi có thể sử dụng phông chữ tùy chỉnh không ?? – Wanderer

+0

Thankyou rất nhiều @Shai –

+0

Bạn không thể tùy chỉnh phông chữ dễ dàng vì phông chữ tùy chỉnh cần được hỗ trợ bởi ứng dụng thư khách. Rất nhiều ứng dụng thư vẫn sử dụng loại HTML trước rất cơ bản của IE6 để chúng sẽ mặc định thành phông chữ khác. –

18

này làm việc cho tôi

msg.BodyFormat = MailFormat.Html; 

và sau đó bạn có thể sử dụng html trong cơ thể bạn

msg.Body = "<em>It's great to use HTML in mail!!</em>" 
+7

Tính năng này hoạt động cho 'MailMessage' trong [' System.Web.Mail'] (https://msdn.microsoft.com/en-us/library/system.web.mail.mailmessage.bodyformat%28v=vs.110% 29.aspx). Làm 'IsBodyHtml' là dành cho một trong' System.Net.Mail' (https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.isbodyhtml (v = vs.110)) .aspx) –

60

Cách tốt nhất để gửi html có định dạng Email

Mã này sẽ nằm trong "Customer.htm"

<table> 
    <tr> 
     <td> 
      Dealer's Company Name 
     </td> 
     <td> 
      : 
     </td> 
     <td> 
      #DealerCompanyName# 
     </td> 
    </tr> 
</table> 

đọc file HTML Sử dụng System.IO.File.ReadAllText. nhận tất cả mã HTML trong biến chuỗi.

string Body = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("EmailTemplates/Customer.htm")); 

Thay thế chuỗi đặc biệt thành giá trị tùy chỉnh của bạn.

Body = Body.Replace("#DealerCompanyName#", _lstGetDealerRoleAndContactInfoByCompanyIDResult[0].CompanyName); 

gọi SendEmail (chuỗi nội dung) Chức năng và thực hiện thủ tục gửi email.

public static void SendEmail(string Body) 
     { 
      MailMessage message = new MailMessage(); 
      message.From = new MailAddress(Session["Email"].Tostring()); 
      message.To.Add(ConfigurationSettings.AppSettings["RequesEmail"].ToString()); 
      message.Subject = "Request from " + SessionFactory.CurrentCompany.CompanyName + " to add a new supplier"; 
      message.IsBodyHtml = true; 
      message.Body = Body; 

      SmtpClient smtpClient = new SmtpClient(); 
      smtpClient.UseDefaultCredentials = true; 

      smtpClient.Host = ConfigurationSettings.AppSettings["SMTP"].ToString(); 
      smtpClient.Port = Convert.ToInt32(ConfigurationSettings.AppSettings["PORT"].ToString()); 
      smtpClient.EnableSsl = true; 
      smtpClient.Credentials = new System.Net.NetworkCredential(ConfigurationSettings.AppSettings["USERNAME"].ToString(), ConfigurationSettings.AppSettings["PASSWORD"].ToString()); 
      smtpClient.Send(message); 
     } 
+3

Có thể sử dụng ** MailDefinition ** hoặc ** RazorEngine ** *** http: //stackoverflow.com/questions/10512845/how-to-send-email-wth-email-template-c-sharp *** – Kiquenet

+0

Cảm ơn bạn - điều này làm việc cho tôi và đã tiết kiệm cho tôi giờ. –

Các vấn đề liên quan