在 C# 中生成 HTML 电子邮件正文 [英] Generating HTML email body in C#

查看:42
本文介绍了在 C# 中生成 HTML 电子邮件正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与使用 Stringbuilder 执行以下操作相比,是否有更好的方法在 C# 中生成 HTML 电子邮件(用于通过 System.Net.Mail 发送):

Is there a better way to generate HTML email in C# (for sending via System.Net.Mail), than using a Stringbuilder to do the following:

string userName = "John Doe";
StringBuilder mailBody = new StringBuilder();
mailBody.AppendFormat("<h1>Heading Here</h1>");
mailBody.AppendFormat("Dear {0}," userName);
mailBody.AppendFormat("<br />");
mailBody.AppendFormat("<p>First part of the email body goes here</p>");

等等,等等?

推荐答案

您可以使用 MailDefinition 类.

这是你如何使用它:

MailDefinition md = new MailDefinition();
md.From = "test@domain.com";
md.IsBodyHtml = true;
md.Subject = "Test of MailDefinition";

ListDictionary replacements = new ListDictionary();
replacements.Add("{name}", "Martin");
replacements.Add("{country}", "Denmark");

string body = "<div>Hello {name} You're from {country}.</div>";

MailMessage msg = md.CreateMailMessage("you@anywhere.com", replacements, body, new System.Web.UI.Control());

另外,我写了一篇关于如何使用 MailDefinition 类使用模板在 C# 中生成 HTML 电子邮件正文.

Also, I've written a blog post on how to generate HTML e-mail body in C# using templates using the MailDefinition class.

这篇关于在 C# 中生成 HTML 电子邮件正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆