gpt4 book ai didi

c# - HTML 表格在邮件中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 11:11:50 25 4
gpt4 key购买 nike

我正在创建一个运行时 HTML 表并通过邮件发送。表格被添加到邮件正文中。但是当邮件被触发时,它显示的是 System.Web.UI.HtmlControls.HtmlTable 而不是表格的内容。

//.csPage:

HtmlTable tbl = new HtmlTable();
tbl.Border = 1;
tbl.CellSpacing = 0;
HtmlTableRow trHeader = new HtmlTableRow();
HtmlTableCell tcHeader = new HtmlTableCell();
tcHeader.InnerText = "Mobile Number";
HtmlTableCell tc1Header = new HtmlTableCell();
tc1Header.InnerText = "Carrier";
trHeader.Cells.Add(tcHeader);
trHeader.Cells.Add(tc1Header);
tbl.Rows.Add(trHeader);
for (int i = 0; i < dvNumbers.Rows.Count; i++)
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell tc = new HtmlTableCell();
tc.InnerText = dvNumbers.Rows[i].Cells[0].Text;
tr.Cells.Add(tc);
HtmlTableCell tc1 = new HtmlTableCell();
tc1.InnerText = dvNumbers.Rows[i].Cells[1].Text;
tr.Cells.Add(tc1);
tbl.Rows.Add(tr);

}

objMail.MobileDeactivationMail(txtMailTo.Text, txtMailCC.Text, txtMailBCC.Text, tbl);

//邮件代码:

public bool MobileDeactivationMail(string strReceiverMailId, string CC, string BCC, HtmlTable tbl)
{
bool blnIsMailSent = false;
MailMessage objMailMsg;
try
{
objMailMsg = new MailMessage();
MailAddress FromAdd = new MailAddress("x@x.com");
objMailMsg.From = FromAdd;
objMailMsg.To.Add(new MailAddress(strReceiverMailId));
if (CC != "")
objMailMsg.CC.Add(new MailAddress(CC));
if (BCC != "")
objMailMsg.Bcc.Add(new MailAddress(BCC));
objMailMsg.Subject = "Deactivation of Mobile Numbers";
objMailMsg.Priority = MailPriority.High;
objMailMsg.IsBodyHtml = true;
objMailMsg.Body = "<table style='font-family:Tahoma; font-size:small';><tr><td> Dear Sir/Mam</td<</tr>," + "<tr><td></td></tr><tr><td></td><td><td><td> Kindly Deactivate the following mobile numbers.</br></br>" + tbl + " </td></td></td></tr></table><table style='font-family:Tahoma; font-size:small';><tr><td>Regards,</td></tr><tr><td>HR</td></tr></table>";
SmtpClient SmtpMail = new SmtpClient();
SmtpMail.Port = 25;
SmtpMail.Host = "xxxxx";
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("xxxxxxx", "xxxxx");
SmtpMail.UseDefaultCredentials = false;
SmtpMail.Credentials = SMTPUserInfo;
SmtpMail.Send(objMailMsg);
objMailMsg.Dispose();
blnIsMailSent = true;
}
catch (Exception Ex)
{
blnIsMailSent = false;
}

return blnIsMailSent;
}

我哪里错了?

最佳答案

你的问题出在这一行:

objMailMsg.Body = "<table style='font-family:Tahoma; font-size:small';>
<tr><td> Dear Sir/Mam</td<</tr>," + "<tr><td></td></tr>
<tr><td></td><td><td><td>Kindly Deactivate the following mobile numbers.</br></br>" + tbl + " </td></td></td></tr></table>
<table style='font-family:Tahoma; font-size:small';><tr><td>Regards,</td></tr><tr><td>HR</td> </tr></table>";

具体来说:

" + tbl + "

当您将对象连接到字符串中时,它将调用 tbl.ToString()

默认情况下,这将返回类名 - 而不是您想要的表格的 HTML。

使用 HTML 表格解决这个问题的方法是直接写出 HTML。不幸的是,尽管存在 InnerText 和 InnerHTML,但此控件 (MSDN HTMLTable Properties) 不支持该控件,因此您必须使用 RenderControl (MSDN RenderControl documentation) 方法:

        StringWriter writer = new StringWriter(); 
Html32TextWriter htmlWriter = new Html32TextWriter(writer);
tbl.RenderControl(htmlWriter);
string html = writer.ToString();

然后您可以连接此 HTML 字符串而不是 tbl

关于c# - HTML 表格在邮件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13701694/

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