我在控制台应用程序中列出了一些从电子邮件中提取的数据。发送包含所有这些列出数据的电子邮件后。问题是数据的结构不是很好,电子邮件也不容易理解。我尝试使用body html但失败了。我想知道是否有人可以帮我弄清楚如何构建电子邮件。下面是我在c#中的代码
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;
namespace sql_connection
{
class Program
{
static void Main(string[] args)
{
string conn = null;
SqlConnection connection;
conn = ("Data Source=Database\\SQL2012;Initial Catalog=jobs;User ID=user;Password=passs");
connection = new SqlConnection(conn);
try
{
connection.Open();
Console.WriteLine("Connection Open!");
SqlCommand cmd = new SqlCommand("SELECT jobs.[dbo].[tb_work].whd_Date,jobs.[dbo].[tb_work].whd_FromTime,jobs.[dbo].[tb_work].whd_ToTime, jobs.[dbo].[tb_work].whd_User,jobs.[dbo].[tb_UserLogin].login_Email FROM jobs.[dbo].[tb_work]INNER JOIN jobs.[dbo].[tb_UserLogin] ON jobs.[dbo].[tb_work].whd_User = jobs.[dbo].[tb_UserLogin].login_LoginId WHERE DATEDIFF(DAY,[whd_FromTime],GETDATE())<=7 AND (whd_ToTime = '' OR whd_ToTime IS NULL) AND(whd_User=login_LoginId)");
cmd.Connection = connection;
SqlDataReader reader = cmd.ExecuteReader();
var columns = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToList();
var list = new List<string>();
var col = new List<string>();
while(reader.Read())
{
var s = string.Format(" {1} {0} {2} {3} ",
reader["whd_ToTime"] == DBNull.Value
? "NULL" : reader["whd_ToTime"].ToString(),
reader["whd_FromTime"] == DBNull.Value
? "NULL" : reader["whd_FromTime"].ToString(),
reader["whd_User"].ToString(),
reader["login_Email"].ToString());
Console.WriteLine(string.Join(" ", columns.ToArray()));
Console.WriteLine(s);
list.Add(s);
}
var sb = new StringBuilder();
foreach (var s in list)
{
sb.AppendLine(s);
}
connection.Close();
MailMessage message;
message=new MailMessage();
MailAddress to = new MailAddress("xxxx@gmail.com");
MailAddress from = new MailAddress("xxxx@gmail.com");
MailMessage mail = new MailMessage(from, to);
mail.Subject = ("missed punch clock");
message.IsBodyHtml = true;
StringBuilder html = new StringBuilder();
html.AppendFormat("<!DOCTYPE html>");
html.AppendFormat("<html><body><table>");
html.AppendFormat("<tr><td>");
html.Append("<table width=600px border=1 cellspacing=2 cellpadding=2 align=center bgcolor=White dir=ltr rules=all style=border-width: thin; border-style: solid; line-height: normal; vertical-align: baseline; text-align: center; font-family: Calibri; font-size: medium; font-weight: normal; font-style: normal; font-variant: normal; color: #000000; list-style-type: none;>");
for (int rowind = 0; rowind < 1; rowind++)
{
html.Append("<tr>");
html.Append("<td>");
html.Append("<table width=600px border=1 cellspacing=2 cellpadding=2 align=center bgcolor=White dir=ltr rules=all style=border-width: thin; border-style: solid; line-height: normal; vertical-align: baseline; text-align: center; font-family: Calibri; font-size: medium; font-weight: normal; font-style: normal; font-variant: normal; color: #000000; list-style-type: none;>");
for (int newrowind = 0; newrowind < 1; newrowind++)
{
html.AppendFormat("<tr>");
html.Append("<td colspan=1 style=font-weight:bold>");
html.Append("whd_ToTime");
html.Append("</td>");
html.Append("<td colspan=2 style=font-weight:bold>");
html.Append("whd_FromTime");
html.Append("</td>");
html.Append("<td colspan=3 style=font-weight:bold>");
html.Append("whd_User");
html.Append("</td>");
html.Append("</tr>");
foreach (var s in list)
{
html.AppendFormat("<tr>");
html.Append("<td colspan=1 style=font-weight:bold>");
html.Append(sb.ToString());
html.Append("</td>");
html.Append("</tr>");
}
}
html.Append("</tr>");
html.Append("</td>");
html.Append("</table>");
}
html.Append("</table>");
html.AppendFormat("</td></tr>");
html.AppendFormat("</table></html></body>");
mail.Body= html.ToString();
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new NetworkCredential("xxxx"gmail.com", xxxxxxxx");
smtp.EnableSsl = true;
Console.WriteLine("Sending email..");
smtp.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
答案 0 :(得分:0)
首先,为了查明您的问题,只需查看生成的HTML,就像您可以检测生成它的代码中的问题一样。你可以做的是首先用HTML设计邮件,当它看起来很好时,编写你的代码来实现它。
您可以更轻松地阅读在代码中加载的HTML模板(作为嵌入式资源),并使用占位符作为自定义数据,而不是在代码中构建HTML - 因为这样就可以将视图(HTML)与逻辑分开。
请注意,格式化邮件是一项艰巨的任务,因为您必须考虑各种设备和邮件客户端。如果您希望它们能够很好地显示在所有这些框架上,那么您最好使用Zurb Ink之类的框架而不是自己做所有事情 - 这样您就有机会最终获得适用于大多数设备和客户端的响应式电子邮件。