c# - 如何使用 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;
}
解决方案
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>";
推荐阅读
- junit - java.lang.IllegalArgumentException:没有足够的变量值可用于扩展“id”错误
- oracle - 在 PL/SQL 中为替换变量提供多个值
- php - 如何使用简单的 HTML Dom 获取图像元素旁边的文本?
- vaadin8 - vaadin 8 中的绑定和解除绑定事件
- ios - 在新模拟器上运行时关闭 iOS 13 键盘教程
- python - TKinter,将小部件放置在与背景图像对齐的窗口的某些部分
- c# - JsonStringEnumConverter (System.Text.Json) 是否支持空值?
- swift - 应用委托访问环境对象
- ios - 为什么 Flutter 的 CupertinoNavigationBar 与 CupertinoPageScaffold 中的孩子重叠?
- azure-cosmosdb - 连接到多个 CosmosDB 文档