通过电子邮件发送表格

我需要在电子邮件中发送查询结果。 我使用两种方法:

GetDataTable() :执行查询并获取数据表(需要在电子邮件中发送)

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

问题:我需要在电子邮件中发送数据表或html表,如下面的代码。 这适用于代替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) { } } 

好的,现在试试这个:

 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 = "The following are the records: 

"; if (dataSet.Tables[0].Rows.Count == 0) return messageBody; string htmlTableStart = ""; string htmlTableEnd = "
"; string htmlHeaderRowStart = ""; string htmlHeaderRowEnd = ""; string htmlTrStart = ""; string htmlTrEnd = ""; string htmlTdStart = ""; string htmlTdEnd = ""; 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) { } }

在过去,我创建了一个inheritance自GridView的对象EmailGrid.cs。 然后使用如下方法将HTML呈现为字符串。

  public string RenderControl() { StringBuilder stringBuilder = new StringBuilder(); StringWriter stringWriter = new StringWriter(stringBuilder); HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter); RenderControl(htmlTextWriter); return stringBuilder.ToString(); } 

如果你想做同样的事情,但是通过DataAdapter循环通过数据表,请查看此链接以获得一个快速示例..因为你几乎做了同样的事情,这个例子显示你试图传递整个数据表的例外情况vs将结果构建到电子邮件正文中。 如何通过电子邮件将DataAdapter用于DataTable