发送表中的电子邮件 [英] Send a table in email

查看:206
本文介绍了发送表中的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,发送一个查询结果的电子邮件。我使用两种方法:

GetDataTable():执行查询和获取数据表(这需要以电子邮件发送)

SendAutomatedEmail():发送自动电子邮件

问题:我需要发送的数据表或HTML表中的电子邮件,类似下面code。这工作正常的字符串代替DataTable的

 公共静态无效的主要(字串[] args)
{
    数据表数据表= GetDataTable();
    SendAutomatedEmail(数据表);
}

    公共静态数据表GetDataTable(字符串的CommandText)
    {
        字符串cnString = ConfigurationManager.ConnectionStrings [连接2]的ConnectionString。
        的SqlConnection的SqlConnection =新的SqlConnection(cnString);

        字符串的CommandText =SELECT * FROM dbo.fs010100(NOLOCK);
        的SqlCommand的SqlCommand =新的SqlCommand(CommandText中,SqlConnection的);

        SqlDataAdapter的SqlDataAdapter的=新System.Data.SqlClient.SqlDataAdapter();
        sqlDataAdapter.SelectCommand = SqlCommand的;

        DataTable中的dataTable =新的DataTable();
        dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;

        //添加或刷新行的数据集,以匹配数据源中的
        尝试
        {
            sqlDataAdapter.Fill(dataTable的);
            在SQLConnection.close(dataTable的);
        }
        赶上(例外_Exception)
        {
            在SQLConnection.close();
            //Console.WriteLine(_Exception.Message);
            返回null;
        }

        返回的dataTable;
    }


    公共静态无效SendAutomatedEmail(DataTable的DT,串收件人=user@domain.com)
    {
        尝试
        {
            字符串的邮件服务器=server.com;

            MailMessage消息=新MailMessage(
                                                   it@domain.com
                                                   接受者,
                                                   测试电子邮件,
                                                   dt.ToString()
                                                   );
            SmtpClient客户端=新SmtpClient(邮件服务器);
            VAR AuthenticationDetails =新的NetworkCredential(user@domain.com,密码);
            client.Credentials = AuthenticationDetails;
            client.Send(消息);
        }
        赶上(例外五)
        {

        }

    }
 

解决方案

好了,现在试试这个:

 公共静态无效的主要(字串[] args)
{
    数据集的数据集= getDataSet();
    字符串htmlString = getHtml(数据集);
    SendAutomatedEmail(htmlStringemail@domain.com);
}

公共静态数据集getDataSet(字符串的CommandText)
{
    字符串cnString = ConfigurationManager.ConnectionStrings [连接2]的ConnectionString。
    的SqlConnection的SqlConnection =新的SqlConnection(cnString);

    字符串的CommandText =SELECT * FROM dbo.fs010100(NOLOCK);
    的SqlCommand的SqlCommand =新的SqlCommand(CommandText中,SqlConnection的);

    SqlDataAdapter的SqlDataAdapter的=新System.Data.SqlClient.SqlDataAdapter();
    sqlDataAdapter.SelectCommand = SqlCommand的;

    数据集的数据集=新的DataSet();

    尝试
    {

        sqlDataAdapter.Fill(数据集,头);
        在SQLConnection.close();
    }
    赶上(例外_Exception)
    {
        在SQLConnection.close();

        返回null;
    }

    返回的数据集;

}


公共静态字符串getHtml(数据集数据集)
{
    尝试
    {
         串消息体=&其中;字体>将以下是记录:其中; /字体>&所述峰; br>&所述峰; br>中;

         如果(dataSet.Tables [0] .Rows.Count == 0)
             返回消息体;
         字符串htmlTableStart =<表格样式= \边界崩溃:崩溃;文本对齐:中心; \>中;
         字符串htmlTableEnd =< /表>;
         字符串htmlHeaderRowStart =< TR风格= \背景颜色:#6FA1D2;颜色:#FFFFFF; \>中;
         串htmlHeaderRowEnd =&其中; / TR>中;
         字符串htmlTrStart =< TR风格= \颜色:#555555; \>中;
         串htmlTrEnd =&其中; / TR>中;
         字符串htmlTdStart =< TD风格= \边框颜色:#5c87b2;边框风格:固体;边框宽度:薄;填充:5px的; \>中;
         串htmlTdEnd =&其中; / TD>中;

         消息体+ = htmlTableStart;
         消息体+ = htmlHeaderRowStart;
         消息体+ = htmlTdStart +列1+ htmlTdEnd;
         消息体+ = htmlHeaderRowEnd;

         的foreach(DataRow中排在notShippedDataSet.Tables [0] .Rows)
         {
             消息体=消息体+ htmlTrStart;
             消息体=消息体+ htmlTdStart +行[字段名] + htmlTdEnd;
             消息体=消息体+ htmlTrEnd;
         }
         消息体=消息体+ htmlTableEnd;


         返回消息体;
     }
     赶上(例外前)
     {
          返回null;
     }
 }

公共静态无效SendAutomatedEmail(字符串htmlString,串收件人=user@domain.com)

{
 尝试
 {
     字符串的邮件服务器=server.com;

     MailMessage消息=新MailMessage(it@domain.com,收件人);
     消息.IsBodyHtml = TRUE;
     消息。体= htmlString;
     消息.Subject =测试邮件;

     SmtpClient客户端=新SmtpClient(邮件服务器);
     VAR AuthenticationDetails =新的NetworkCredential(user@domain.com,密码);
     client.Credentials = AuthenticationDetails;
     client.Send(消息);
 }
 赶上(例外五)
 {

 }

}
 

I have a requirement to send the results of a query in emails. I am using two methods:

GetDataTable() : to execute the query and obtain datatable(which needs to be sent in email)

SendAutomatedEmail() : to send automated emails.

Problem: i need to send data table or html table in email, something like code below. this works fine for a string in place of dataTable

public static void Main(string[] args)
{
    DataTable datatable = GetDataTable();
    SendAutomatedEmail(datatable );
}

    public static DataTable GetDataTable(string CommandText)
    {
        string cnString = ConfigurationManager.ConnectionStrings["Connection2"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(cnString);

        string CommandText = "select * from dbo.fs010100 (nolock)";
        SqlCommand sqlCommand =  new SqlCommand( CommandText, sqlConnection);

        SqlDataAdapter sqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
        sqlDataAdapter.SelectCommand = sqlCommand;

        DataTable dataTable = new DataTable();
        dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;

        // Adds or refreshes rows in the DataSet to match those in the data source
        try
        {
            sqlDataAdapter.Fill(dataTable);
            sqlConnection.Close(dataTable );
        }
        catch (Exception _Exception)
        {
            sqlConnection.Close();
            //Console.WriteLine(_Exception.Message);
            return null;
        }

        return dataTable;
    }


    public static void SendAutomatedEmail(DataTable dt, string recipient = "user@domain.com")
    {
        try
        {
            string mailServer = "server.com";

            MailMessage message = new MailMessage(
                                                   "it@domain.com",
                                                   recipient,
                                                   "Test Email",
                                                   dt.ToString()
                                                   );
            SmtpClient client = new SmtpClient(mailServer);
            var AuthenticationDetails = new NetworkCredential("user@domain.com", "password");
            client.Credentials = AuthenticationDetails;
            client.Send(message);
        }
        catch (Exception e)
        {

        }

    }

解决方案

ok, try this now:

public static void Main(string[] args)
{
    DataSet dataSet = getDataSet();
    string htmlString= getHtml(dataSet);
    SendAutomatedEmail(htmlString, "email@domain.com");
}

public static DataSet getDataSet(string CommandText)
{
    string cnString = ConfigurationManager.ConnectionStrings["Connection2"].ConnectionString;
    SqlConnection sqlConnection = new SqlConnection(cnString);

    string CommandText = "select * from dbo.fs010100 (nolock)";
    SqlCommand sqlCommand =  new SqlCommand( CommandText, sqlConnection);

    SqlDataAdapter sqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
    sqlDataAdapter.SelectCommand = sqlCommand;

    DataSet dataSet = new DataSet();

    try
    {

        sqlDataAdapter.Fill(dataSet, "header");
        sqlConnection.Close();
    }
    catch (Exception _Exception)
    {
        sqlConnection.Close();

        return null;
    }

    return dataSet;

}


public static string getHtml(DataSet dataSet)
{
    try
    {
         string messageBody = "<font>The following are the records: </font><br><br>";

         if (dataSet.Tables[0].Rows.Count == 0)
             return messageBody;
         string htmlTableStart = "<table style=\"border-collapse:collapse; text-align:center;\" >";
         string htmlTableEnd = "</table>";
         string htmlHeaderRowStart = "<tr style =\"background-color:#6FA1D2; color:#ffffff;\">";
         string htmlHeaderRowEnd = "</tr>";
         string htmlTrStart = "<tr style =\"color:#555555;\">";
         string htmlTrEnd = "</tr>";
         string htmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">";
         string htmlTdEnd = "</td>";

         messageBody+= htmlTableStart;
         messageBody += htmlHeaderRowStart;
         messageBody += htmlTdStart + "Column1 " + htmlTdEnd;
         messageBody += htmlHeaderRowEnd;

         foreach (DataRow Row in notShippedDataSet.Tables[0].Rows)
         {
             messageBody = messageBody + htmlTrStart;
             messageBody = messageBody + htmlTdStart + Row["fieldName"] + htmlTdEnd;
             messageBody = messageBody + htmlTrEnd;
         }
         messageBody = messageBody + htmlTableEnd;


         return messageBody;
     }
     catch (Exception ex)
     {
          return null;
     }
 }

public static void SendAutomatedEmail(string htmlString, string recipient = "user@domain.com")

{
 try
 {
     string mailServer = "server.com";

     MailMessage message = new MailMessage("it@domain.com", recipient);
     message .IsBodyHtml = true;
     message .Body = htmlString;
     message .Subject = "Test Email";

     SmtpClient client = new SmtpClient(mailServer);
     var AuthenticationDetails = new NetworkCredential("user@domain.com", "password");
     client.Credentials = AuthenticationDetails;
     client.Send(message);
 }
 catch (Exception e)
 {

 }

}

这篇关于发送表中的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆