gpt4 book ai didi

c# - String.Format 在 HTML 电子邮件模板的样式部分失败

转载 作者:行者123 更新时间:2023-11-30 14:46:03 24 4
gpt4 key购买 nike

我正在使用 Bodybuilder 和邮件发送服务发送 HTML 电子邮件模板。我用这个链接学习。

https://www.c-sharpcorner.com/article/send-email-using-templates-in-asp-net-core-applications/

无论如何,我使用外部服务 ( https://topol.io/ ) 创建了我的电子邮件模板。

当我尝试将生成的 HTML 文件与第一个链接中引用的 String.FOrmat 命令一起使用时,出现错误。我终于弄清楚是这部分代码导致 String.Format 失败。

这是我的 C# 代码,通常可以与其他 HTML 模板一起使用:

using (StreamReader SourceReader = System.IO.File.OpenText(pathToFile))
{
builder.HtmlBody = SourceReader.ReadToEnd();
}

string messagebody = string.Format(builder.HtmlBody,
//Not important
);

现在,HTML 中的这种样式代码导致 String.Format 崩溃(或者 bodybuilder,我不确定到底是哪一个有问题,它在 string.format 处失败)。

<style type="text/css">
#outlook a {
padding: 0;
}

.ReadMsgBody {
width: 100%;
}

.ExternalClass {
width: 100%;
}

.ExternalClass * {
line-height: 100%;
}

body {
margin: 0;
padding: 0;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}

table, td {
border-collapse: collapse;
mso-table-lspace: 0pt;
mso-table-rspace: 0pt;
}

img {
border: 0;
height: auto;
line-height: 100%;
outline: none;
text-decoration: none;
-ms-interpolation-mode: bicubic;
}

p {
display: block;
margin: 13px 0;
}
</style>
<!--[if !mso]><!-->
<style type="text/css">
@media only screen and (max-width:480px) {
@-ms-viewport {
width: 320px;
}

@viewport {
width: 320px;
}
}
</style>
<style type="text/css">
@media only screen and (min-width:480px) {
.mj-column-per-100 {
width: 100% !important;
}

.mj-column-per-60 {
width: 60% !important;
}

.mj-column-per-40 {
width: 40% !important;
}

.mj-column-per-50 {
width: 50% !important;
}
}
</style>
<!--[if mso]><xml>
<o:OfficeDocumentSettings>
<o:AllowPNG/>
<o:PixelsPerInch>96</o:PixelsPerInch>
</o:OfficeDocumentSettings></xml><![endif]-->
<!--[if lte mso 11]><style type="text/css">.outlook-group-fix { width:100% !important; }</style>
<![endif]-->
<!--[if !mso]><!-->
<link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Merriweather" rel="stylesheet" type="text/css">
<style type="text/css">
@import url(https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700);
@import url(https://fonts.googleapis.com/css?family=Merriweather);
</style>
<!--<![endif]-->

有什么想法吗?如果失败的原因不明显,我愿意接受有关如何将此 CSS 部分放在外部的建议。

最佳答案

您收到该错误是因为在 string.Format 中使用花括号作为值参数的占位符。

解决问题的一种方法是用双大括号替换大括号:

using (StreamReader SourceReader = System.IO.File.OpenText(pathToFile))
{
builder.HtmlBody = SourceReader.ReadToEnd();
}

string htmlBody = builder.HtmlBody.Replace("{", "{{").Replace("}","}}")
string messagebody = string.Format(htmlBody,
//Not important
);

关于c# - String.Format 在 HTML 电子邮件模板的样式部分失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50852462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com