如何发送 HTML 格式的电子邮件?
问题描述:
我可以让 Web 应用程序使用 Windows 任务计划程序自动发送电子邮件.现在我想使用我为发送电子邮件编写的以下方法发送 HTML 格式的电子邮件.
I could be able to let the web application sends automatic emails using Windows Task Scheduler. Now I want to send HTML-Formatted email using the following method that I wrote for sending emails.
我的代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
SmtpClient sc = new SmtpClient("mail address");
MailMessage msg = null;
try
{
msg = new MailMessage("xxxx@gmail.com",
"yyyy@gmail.com", "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();
}
}
}
怎么做?我只想在电子邮件中添加一些带有一个链接的粗体文本,也许还有一张图片.
How to do that? I just want to put some bold text with one link and maybe one image in the email.
答
将 isBodyHtml
设置为 true
允许您在消息正文中使用 HTML 标记:
Setting isBodyHtml
to true
allows you to use HTML tags in the message body:
msg = new MailMessage("xxxx@gmail.com",
"yyyy@gmail.com", "Message from PSSP System",
"This email sent by the PSSP system<br />" +
"<b>this is bold text!</b>");
msg.IsBodyHtml = true;