我正在使用邮件在C#中发送电子邮件,我想在邮件正文中添加一个表格。
我正在使用一个字符串变量来存储html代码,但是它会打印出整个字符串,就像在邮件正文中一样。
string EPT;
string PRINTER;
string CID;
string htmlString= "<!DOCTYPE html>< html >< body > Hi All, < br /> < br /> Please find below, status of PDS SPOS Devices < br />< br /> < table border='1' >< tr >< th > Device Name </ th >< th > Online Status </ th >< th > Device Status </ th >< th > EPT Status </ th >< th > PRINTER Status </ th >< th > CID Status </ th ></ tr > ";
foreach (RetrieveDeviceData deviceData in SPOSStatusList)
{
if (deviceData.EPTStatus.Contains("EPT is not returning any error on commands"))
{
EPT = "EPT OK";
}
else if (!deviceData.EPTStatus.Contains("NA"))
{
EPT = "EPT Not OK";
}
else
{
EPT = "NA";
}
if (deviceData.PRINTERStatus.Contains("PRINTER - OK"))
{
PRINTER = "PRINTER OK";
}
else if (!deviceData.PRINTERStatus.Contains("NA"))
{
PRINTER = "PRINTER Not OK";
}
else
{
PRINTER = "NA";
}
if (deviceData.CIDStatus.Contains("CID-OK"))
{
CID = "CID OK";
}
else if (!deviceData.CIDStatus.Contains("NA"))
{
CID = "CID Not OK";
}
else
{
CID = "NA";
}
htmlString = htmlString + "< tr >< td >" + deviceData.DeviceName.ToString() + "</ td >< td >" + deviceData.OnlineStatus.ToString() + "</ td >< td >" + deviceData.DeviceStatus.ToString() + "</ td >< td >" + EPT.ToString() + "</ td >< td >" + PRINTER.ToString() + "</ td >< td >" + CID.ToString() + "</ td ></ tr >";
}
htmlString = htmlString + "</ table >< br /><br /> Regards < br /> Presto CTSS </ body ></ html>";
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["mailFrom"].ToString());
mailMessage.To.Add(new MailAddress(ConfigurationManager.AppSettings["mailTo"].ToString()));
mailMessage.Subject = "PDS SPOS Device Status Report - " + DateTime.Now;
mailMessage.IsBodyHtml = true;
mailMessage.Body = htmlString;
SmtpClient client = new SmtpClient();
Attachment attachment = new Attachment(reportPath + "\\SPOSStatusReport.csv");
mailMessage.Attachments.Add(attachment);
client.Host = ConfigurationManager.AppSettings["mailServerIp"].ToString();
try
{
client.Send(mailMessage);
writer.WriteLine("Email sent");
}
catch(Exception e)
{
writer.WriteLine(e.ToString());
}
预期结果-带有表数据的邮件正文 上面代码中htmlString变量的实际值按邮件中的原样打印。