【问题标题】:Error Inserting <Table> into Email将 <表格> 插入电子邮件时出错
【发布时间】:2018-05-10 02:16:38
【问题描述】:

我正在尝试在电子邮件中插入表格。我在 6 行代码中有以下错误(以“body”开头,结尾 onavgErrorOutStDev +=“/table”)。错误是:

“赋值的左侧必须是变量、属性或索引器”。

&lt;table&gt; 启动是在创建/发送电子邮件之前执行的方法。如何修复错误?

MailMessage errorOutStanDev = new MailMessage();
        errorOutStanDev.IsBodyHtml = true;
        SmtpClient SmtpServerThree = new SmtpClient("smtp.xxxx.xxx");
        errorOutStanDev.From = new 
System.Net.Mail.MailAddress("PerformanceUpdate@xxx.xxx");
        errorOutStanDev.To.Add(new 
System.Net.Mail.MailAddress("xxxxxx@xxxx.xxx"));
        string htmlBody = "<body>" +
                          "<p><b> The following errors have occurred more 
 often than others</b></p>" +
                          avgErrorOutStDev += "<tr>" +
                          avgErrorOutStDev += "<td>" + sitename + "</td>"+ 
                          avgErrorOutStDev += "<td>" + avg + "</td>" +
                          avgErrorOutStDev += "</tr>" +
                          avgErrorOutStDev += "</table>" +
                          "</body>";

【问题讨论】:

  • "" + x += "" 没有意义。
  • @SLaks 刚刚编辑
  • 错误试图告诉你你的代码没有意义。
  • @SLaks 如何让代码工作?我曾尝试使用前一种方法填充表格,但随后我得到 10 个重复条目。
  • 要么在一个语句中使用+,要么在多个语句中使用+=。您需要学习 C# 语法和字符串连接的基础知识。

标签: c# html-table smtp html-email


【解决方案1】:

在构建主体时使用 StringBuilder。代码看起来更干净。这应该可以。

    string avgErrorOutStDev = string.Empty;
                avgErrorOutStDev += "<tr>";
                avgErrorOutStDev += "<td>" + sitename + "</td>";
                avgErrorOutStDev += "<td>" + avg + "</td>";
                avgErrorOutStDev += "</tr>";
                avgErrorOutStDev += "</table>";

                StringBuilder htmlBody = new StringBuilder();
                htmlBody.AppendLine("<body>");
                htmlBody.AppendLine("<p><b> The following errors have occurred more often than others </ b ></ p >");
                htmlBody.AppendLine(avgErrorOutStDev);
                htmlBody.AppendLine("</ body >");

                htmlBody.ToString();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多