繁体   English   中英

如何在c#中通过邮件发送数据表列

[英]how to send the datatable column through mail in c#

我有两个单独的功能,一个是获取特定列的计数,另一个是通过 C# 代码发送邮件,两者都运行良好,我想将计数值从数据表发送到邮件正文?有人帮我吗?

program1-从数据表中获取计数值

 public static void push_notify()
        {
            var no = "25/10/2017";

            try
            {
                string connection = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString.ToString();
                SqlConnection con = new SqlConnection(connection);
                SqlCommand cmd = con.CreateCommand();


                cmd.CommandText = "sampleone";



                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@createdOn",no);

                cmd.Connection = con;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);


                foreach (DataRow row in dt.Rows)
                {
                    Console.WriteLine();
                    for (int x = 0; x < dt.Columns.Count; x++)
                    {
                        Console.Write(row[x].ToString() + " ");
                    }
                }

                con.Close();


            }
            catch(Exception)
            {

            }
        }

程序2-在c中发送邮件

public static void email_send()
        {
            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress("someemail@somedomain.com");
                mail.To.Add("somereceiver@somedomain.com");
                mail.Subject = "Hello World";
                mail.Body = "<h1>Hello</h1>";
                mail.IsBodyHtml = true;
                mail.Attachments.Add(new Attachment("C:\\Users\\vijay\\Downloads\\dancdan.xml"));

                using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
                {
                    smtp.Credentials = new NetworkCredential("someemail@somedomain.com", "somepassword");
                    smtp.EnableSsl = true;
                    smtp.Send(mail);
                }
            }

        }

您的第一个函数应该返回您要通过电子邮件发送的数据,如下所示:

public static List<string> push_notify()
    {
        var no = "25/10/2017";
        var result = new List<string>();

        try
        {
            string connection = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString.ToString();
            SqlConnection con = new SqlConnection(connection);
            SqlCommand cmd = con.CreateCommand();


            cmd.CommandText = "sampleone";



            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@createdOn",no);

            cmd.Connection = con;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);


            foreach (DataRow row in dt.Rows)
            {
                Console.WriteLine();
                for (int x = 0; x < dt.Columns.Count; x++)
                {
                    var value = row[x].ToString();
                    Console.Write(value + " ");
                    result.Add(value);
                }
            }

            con.Close();
            return result;

        }
        catch(Exception)
        {
            return result;
        }
    }

第二个函数应该调用第一个函数,就像这样(假设它们在同一个命名空间中):

public static void email_send()
{
    using (MailMessage mail = new MailMessage())
    {
        var dataToSend = push_notify();

        mail.From = new MailAddress("someemail@somedomain.com");
        mail.To.Add("somereceiver@somedomain.com");
        mail.Subject = "Hello World";
        mail.Body = string.Join(",", dataToSend);
        mail.IsBodyHtml = true;
        mail.Attachments.Add(new Attachment("C:\\Users\\vijay\\Downloads\\dancdan.xml"));

        using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
        {
            smtp.Credentials = new NetworkCredential("someemail@somedomain.com", "somepassword");
            smtp.EnableSsl = true;
            smtp.Send(mail);
        }
    }

}

这应该够了吧。 但是,请尝试考虑您通过评论收到的所有建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM