Html页面到字符串

发布于 2025-01-02 15:08:26 字数 285 浏览 0 评论 0原文

我正在以 HTML 格式向我的用户发送电子邮件。那里有很多编辑内容,比如用户名、用户生日和许多其他详细信息。

现在,我不希望我的代码看起来像这样:

String message = "hello " + "David\n" + "congratulation for your " + birthday + "\nPleas visit our site\n" + siteLink + " to get your bonus";

是否有任何 C# 工具可以用来轻松编辑?

I am sending emails to my users in HTML format. There is lots of editing there, like user name, user birthday and many other details.

Now, I don't want my code to be look like this:

String message = "hello " + "David\n" + "congratulation for your " + birthday + "\nPleas visit our site\n" + siteLink + " to get your bonus";

Is there any C# tools I can use to make it easy editing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

穿透光 2025-01-09 15:08:27

您只需将 html 代码分配给消息正文

message.Body = "<b> This is a bold Text </b>";
message.IsBodyHTML = true;

此外,您还可以利用MailDefinition 类。检查此链接了解此类的用法。

You can simply assign html code to message body

message.Body = "<b> This is a bold Text </b>";
message.IsBodyHTML = true;

Also, you can take advantage of MailDefinition Class. Check this link for the usage of this class.

姐不稀罕 2025-01-09 15:08:27

使用 isHtmlBody 属性,来自 MailMessage

示例:

MailMessage msg = new MailMessage();
msg.Body= "example of <a href='www.something.com'>Link</a>";
msg.IsBodyHtml = True;
...    
smtp.send(msg)

请参阅:

IsBodyHtml属性

示例

Use isHtmlBody property, from MailMessage

Example:

MailMessage msg = new MailMessage();
msg.Body= "example of <a href='www.something.com'>Link</a>";
msg.IsBodyHtml = True;
...    
smtp.send(msg)

see :

IsBodyHtml Property

Example

唯憾梦倾城 2025-01-09 15:08:27

string.Format() 可能就是您所追求的。

string.Format() 将指定字符串中的每个格式项替换为对应对象值的等效文本。

例如

 string.Format("Dear {0} {1}, You are {2} today.",
       person.Title, person.Lastname, person.Age);

string.Format() might be what you are after.

string.Format() replaces each format item in a specified string with the text equivalent of a corresponding object's value.

So for instance

 string.Format("Dear {0} {1}, You are {2} today.",
       person.Title, person.Lastname, person.Age);
临风闻羌笛 2025-01-09 15:08:26

看来您希望能够在单个字符串中指定命名标记,以便 HTML 易于阅读和编辑,如下所示:

"Hello {FirstName},\nPlease visit our site:\n{SiteLink}"

看看这个答案,了解一些实现此目的的方法: C# 中的命名字符串格式

It seems that you want to be able to specify named tokens in a single string so that the HTML is easy to read and edit, like so:

"Hello {FirstName},\nPlease visit our site:\n{SiteLink}"

Take a look at this answer for some ways to do that: Named string formatting in C#.

断念 2025-01-09 15:08:26
string customBody = "<a href=\"www.oursite.com\">www.oursite.com</a>";
string htmlBody = String.Format("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
        body += "<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">";
        body += "</HEAD><BODY><DIV>{0}</DIV></BODY></HTML>", customBody);

var message = new MailMessage(from, to);
var mimeType = new System.Net.Mime.ContentType("text/html");
var alternate = AlternateView.CreateAlternateViewFromString(htmlBody, mimeType);
message.AlternateViews.Add(alternate);        
message.IsBodyHTML = true;
smtpClient.Send(message);

请参阅 MSDN:

string customBody = "<a href=\"www.oursite.com\">www.oursite.com</a>";
string htmlBody = String.Format("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
        body += "<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">";
        body += "</HEAD><BODY><DIV>{0}</DIV></BODY></HTML>", customBody);

var message = new MailMessage(from, to);
var mimeType = new System.Net.Mime.ContentType("text/html");
var alternate = AlternateView.CreateAlternateViewFromString(htmlBody, mimeType);
message.AlternateViews.Add(alternate);        
message.IsBodyHTML = true;
smtpClient.Send(message);

See MSDN:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文