Html 标签在 c# 中没有格式化 string.format
Html tags not formating inside c# 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);
我想收到电子邮件 "email stuff with important Big stuff",但是我收到的电子邮件是:<p>email stuff with <b>important</b> {0} stuff</p>
您可以使用字符串插值直接添加您需要的值:
string title = "Big"
// Now this value has your expected output.
string text = $"<p>email stuff with <b>important</b> {title} stuff</p>";
实际上这取决于您是如何发送电子邮件的。
您应该将文本存储到 MailMessage
的 Body
部分
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);
我正在尝试发送一封电子邮件,其中包含格式化的 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);
我想收到电子邮件 "email stuff with important Big stuff",但是我收到的电子邮件是:<p>email stuff with <b>important</b> {0} stuff</p>
您可以使用字符串插值直接添加您需要的值:
string title = "Big"
// Now this value has your expected output.
string text = $"<p>email stuff with <b>important</b> {title} stuff</p>";
实际上这取决于您是如何发送电子邮件的。
您应该将文本存储到 MailMessage
Body
部分
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);