如何使用C#通过电子邮件发送HTML内容

时间:2018-07-25 08:28:30

标签: c# html asp.net email gmail

我有一个HTML页面(使用div标签)如何将正文作为html标签? 与enter image description here相同,我只想邮寄相同格式的收据

代码

using System.IO;  
using System.Net;  
using System.Net.Mail;  


string to = " "; //To address    
string from = ""; //From address    
MailMessage message = new MailMessage(from, to);  

string mailbody = "In this article you will learn how to send a email using Asp.Net & C#";  
message.Subject = "Sending Email Using Asp.Net & C#";  
message.Body = mailbody;  
message.BodyEncoding = Encoding.UTF8;  
message.IsBodyHtml = true;  
SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //Gmail smtp    
System.Net.NetworkCredential basicCredential1 = new  
System.Net.NetworkCredential("yourmail id", "Password");  
client.EnableSsl = true;  
client.UseDefaultCredentials = false;  
client.Credentials = basicCredential1;  
try   
{  
    client.Send(message);  
}   

catch (Exception ex)   
{  
    throw ex;  
}

1 个答案:

答案 0 :(得分:0)

home.html已设置为message.IsBodyHtml = true;,因此您只需将邮件正文替换为html格式的文本,例如:

true