在电子邮件中显示列名和电子邮件结构

时间:2015-05-27 12:53:09

标签: c# database email

所以我正在尝试发送一封包含来自数据库的信息的电子邮件。我有一个问题,因为当我发送电子邮件时,列名称不显示。另外,我如何构建电子邮件以显示像数据库意义列名称,然后在其下面的信息然后下一列。下面是我的代码。你可以在底部看到我做了columns.tostring但是没有用。任何帮助表示赞赏

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);
    }                       
}
}
}

1 个答案:

答案 0 :(得分:0)

在列表上调用ToString()不会为您提供列表内容。 请改用:

string columnNames = string.Join(',', columns);
mail.Subject = ("missed punch clock");        
mail.Body = String.Concat(sb.ToString(), columnNames );