提问人:Ashley Ferns 提问时间:11/9/2023 更新时间:11/9/2023 访问量:21
我想从 asp.net Web 表单发送邮件。问题是它不会从后端获取数据以在邮件中显示
I want to send mail from asp.net web forms. The problem is that it doesn't fetch the data from backend to show it in the mail
问:
我想在我的 html 电子邮件中填充 json 数据。我不知道表格没有显示在邮件中的原因。
.cs 代码:
StringBuilder emailBody = new StringBuilder(); string templatePath = HttpContext.Current.Server.MapPath("~/Mail_Template/AppEmail.html"); string emailTemplate = File.ReadAllText(templatePath); StringBuilder tableRows = new StringBuilder(); foreach (var item in jsonData) { emailBody.AppendLine("<tr>"); emailBody.AppendLine($"<td>{item.App_name}</td>"); emailBody.AppendLine($"<td>{item.From_date}</td>"); emailBody.AppendLine($"<td>{item.To_date}</td>"); emailBody.AppendLine("</tr>"); } string emailBodytoSend = templatePath.Replace("{APPLICATION_ROWS}", tableRows.ToString()); mailMessage.Body = emailBodytoSend.ToString(); mailMessage.Body = emailTemplate; mailMessage.IsBodyHtml = true; smtpClient.Send(mailMessage); }
Html 模板:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <table width="100%" cellspacing="0" cellpadding="0"> <tr> <td align="center"> <h1>Your Email Title</h1> <p>Hello,</p> <p>This is a sample email template for Application Details.</p> <!-- You can include dynamic data here --> <!--<p>Application Name: {APPLICATION_NAME}</p> <p>From Date: {FROM_DATE}</p> <p>To Date: {TO_DATE}</p>--> <table border="1" cellpadding="10"> <tr> <th>Application Name</th> <th>From Date</th> <th>To Date</th> </tr> {APPLICATION_ROWS} </table> <p>You can track the same.</p> <p>Best regards,</p> <p>Your Name</p> </td> </tr> </table> </body> </html>
答:
像这样更改代码:
string templatePath = HttpContext.Current.Server.MapPath("~/Mail_Template/AppEmail.html");
string emailTemplate = File.ReadAllText(templatePath);
StringBuilder tableRows = new StringBuilder();
foreach (var item in jsonData) {
StringBuilder tr = new StringBuilder();
tr.AppendLine("<tr>");
tr.AppendLine($"<td>{item.App_name}</td>");
tr.AppendLine($"<td>{item.From_date}</td>");
tr.AppendLine($"<td>{item.To_date}</td>");
tr.AppendLine("</tr>");
tableRows.Append(tr.ToString());
}
string emailBodytoSend = templatePath.Replace("{APPLICATION_ROWS}", tableRows.ToString());
mailMessage.Body = emailBodytoSend.ToString();
mailMessage.Body = emailTemplate;
mailMessage.IsBodyHtml = true;
smtpClient.Send(mailMessage);
问题是您没有将 tr 数据添加到 tableRows 变量中。
上一个:Shopware6 更改搜索逻辑
评论