【问题标题】:String.Format failing on style section of HTML Email TemplateString.Format 在 HTML 电子邮件模板的样式部分失败
【发布时间】:2018-06-14 08:00:54
【问题描述】:

我正在使用 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 崩溃(或者是车身制造商,我不确定到底是哪一个有问题,它在 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 部分放在外部的建议。

【问题讨论】:

  • 解决这个问题最优雅的方法是使用 CSS 内联。如果没有

标签: c# html .net-core string.format bodybuilder.js


【解决方案1】:

您收到该错误是因为 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
                    );

【讨论】:

  • @dave317,每次阅读模板时都这样做是低效的,你怎么知道哪些大括号是占位符,哪些不是占位符。如果没有占位符,则根本不需要String.Format
  • 我同意,但是当我写答案时,我将 OP 的评论 //not important 解释为对问题不重要,但对他的代码很重要。
  • 当然!我正在使用占位符。我应该意识到这一点。 SMH 时刻。我将用双打替换单个大括号,看看它是否有效。我会在模板上做。你是正确的垫子,//不重要的是一些占位符信息。
【解决方案2】:

您正在对包含大量 CSS 定义的字符串调用 String.Format。 CSS 定义包括许多 {} 对。 String.Format{} 对解释为用于替换的占位符,但这不是它们在您的字符串中表示的内容。

为什么在这种情况下需要使用String.Format

为什么不这样做呢,

builder.HtmlBody = File.ReadAllText(pathToFile);

如果//Not important 实际上很重要,您可以转义模板中的大括号as per this answer。这样您不想替换的大括号分别变为'{{''}}'

基本上,在您的模板上再运行一次准备步骤,将{ 切换为{{,将} 切换为}},除非它们代表您要替换的占位符。

【讨论】:

    【解决方案3】:

    问题在于 CSS 部分的 {}。对于string.Format,它们代表占位符,但 CSS 代码不是有效的占位符。

    如果您确实需要用string.Format 替换某些文本并且总是通过string.Format 推送输入文件,您可以简单地将输入中的每个{} 分别替换为{{}} ,您实际上希望 { 出现在最终结果中,并使用 {} 进行替换。但不确定它如何与 Bodybuilder 一起发挥作用。

    【讨论】:

      猜你喜欢
      • 2014-01-03
      • 2013-12-11
      • 1970-01-01
      • 2011-04-15
      • 2013-09-16
      • 2019-12-02
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      相关资源
      最近更新 更多