显示电子邮件中的列名和电子邮件的结构
本文关键字:电子邮件 结构 显示 | 更新日期: 2023-09-27 17:50:51
所以我试图发送一封电子邮件与信息从数据库得到。我有一个问题,因为当我发送电子邮件的列名不显示。此外,我怎么能结构电子邮件显示像一个数据库的含义列名,并在它下的信息,然后下一列。下面是我的代码。正如你在底部看到的,我画了列。拉绳,但不工作。任何帮助都是感激的
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].[table hours].whd_Date,[jobs].[dbo].[table hours].whd_FromTime,[jobs].[dbo].[table hours].whd_ToTime, [jobs].[dbo].[table hours].whd_User,[jobs].[dbo].[table login].login_Email FROM [jobs].[dbo].[table hours]INNER JOIN [jobs].[dbo].[table login] ON [jobs].[dbo].[table hours].whd_User = [jobs].[dbo].[table login].login_LoginId WHERE DATEDIFF(DAY,[whd_Date],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>();
while(reader.Read())
{
var s = string.Format("{4},{3},{2}, {1}, {0}",
reader["whd_ToTime"] == DBNull.Value? "NULL" : reader["whd_ToTime"].ToString(),
reader["whd_FromTime"] == DBNull.Value? "NULL" : reader ["whd_FromTime"].ToString(),
reader["whd_Date"].ToString(),
reader["whd_User"].ToString(),
reader["login_Email"].ToString());
Console.WriteLine("'t", columns.ToArray()));
Console.WriteLine(s);
list.Add(s);
}
var sb = new StringBuilder();
foreach (var s in list)
{
sb.AppendLine(s);
}
connection.Close();
MailAddress to = new MailAddress("email@gmail.com");
MailAddress from = new MailAddress("email@gmail.com");
MailMessage mail = new MailMessage(from, to);
mail.Subject = ("missed punch clock");
mail.Body = sb.ToString(); columns.ToString();
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new NetworkCredential(
"email@gmail.com", "passworrd");
smtp.EnableSsl = true;
Console.WriteLine("Sending email..");
smtp.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
在列表上调用ToString()
不会给您列表的内容。用这个代替:
string columnNames = string.Join(',', columns);
mail.Subject = ("missed punch clock");
mail.Body = String.Concat(sb.ToString(), columnNames );