首页 > 解决方案 > 如何使用 C# 通过电子邮件发送 HTML 内容

问题描述

我有一个 HTML 页面(使用 div 标签) 如何将 body 作为 html 标签?就像在此处输入图像描述我只想邮寄相同格式的收据一样

代码

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;  
}

标签: c#htmlasp.netemailgmail

解决方案


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

string mailbody = "<html><body>In this article you will learn how to send a email using Asp.Net & C#</body></html>";  

推荐阅读