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

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

问题描述



<$ p $

有没有更好的方式在C#(通过System.Net.Mail发送)中生成HTML电子邮件,而不是使用Stringbuilder来执行以下操作: p> string userName =John Doe;
StringBuilder mailBody = new StringBuilder();
mailBody.AppendFormat(< h1>这里标题< / h1>);
mailBody.AppendFormat(Dear {0},userName);
mailBody.AppendFormat(< br />);
mailBody.AppendFormat(< p>电子邮件正文的第一部分在这里< / p>);

等等,等等?

解决方案

您可以使用 MailDefinition class



这是你如何使用它:

  MailDefinition md = new MailDefinition(); 
md.From =test@domain.com;
md.IsBodyHtml = true;
md.Subject =MailDefinition的测试;

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

string body =< div> Hello {name}您来自{country}。< / div>;

MailMessage msg = md.CreateMailMessage(you@anywhere.com,替换,正文,新建System.Web.UI.Control());

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


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>");

and so on, and so forth?

解决方案

You can use the MailDefinition class.

This is how you use it:

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());

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天全站免登陆