Html标签没有在c#string.format中形成

时间:2017-08-15 01:37:00

标签: c# html string.format

我正在尝试在字符串和参数中发送格式化HTML的电子邮件 我的代码是这样的:

string title = "Big";
string text = "<p>email stuff with <b>important</b> {0} stuff</p>";
string.Format(text, title);


MailMessage msz = new MailMessage();
var studentEmail = "someplace@somewhere.net";


msz.To.Add(new MailAddress(studentEmail, "Someone"));

msz.From = new MailAddress(from);                                    

msz.Subject = "Subject";
msz.Body = bodyText;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;

smtp.Send(msz);

我想在电子邮件中收到“带有重要重要内容的电子邮件”,但我在电子邮件中收到此消息:<p>email stuff with <b>important</b> {0} stuff</p>

2 个答案:

答案 0 :(得分:0)

您可以使用 字符串插值 直接添加所需的值:

string title = "Big"

// Now this value has your expected output.
string text = $"<p>email stuff with <b>important</b> {title} stuff</p>";

答案 1 :(得分:-1)

实际上,这取决于你是如何发送电子邮件的。

您应该将文字存储到Body

MailMessage部分
string title = "Big";
string text = "<p>email stuff with <b>important</b> {0} stuff</p>";
string.Format(text, title);


MailMessage message = new MailMessage();
message.From = "sender@abc.com");
message.To.Add("dummy@abc.com");
message.Subject = title;
message.IsBodyHtml = true;
message.Body = text;

使用IsBodyHtml = true,它可以帮助您格式化所有Html标记。

发送:

SmtpClient smtpClient = new SmtpClient()
...
smtpClient.Send(message);