2012-01-10 38 views
5

Tôi có yêu cầu gửi kết quả truy vấn trong email. Tôi đang sử dụng hai phương pháp:Gửi một bảng trong email

GetDataTable(): để thực hiện truy vấn và lấy DataTable (mà cần phải được gửi bằng thư điện tử)

SendAutomatedEmail(): gửi email tự động.

Sự cố: tôi cần gửi bảng dữ liệu hoặc bảng html trong email, giống như mã bên dưới. điều này hoạt động tốt cho một chuỗi thay cho 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 = "[email protected]") 
    { 
     try 
     { 
      string mailServer = "server.com"; 

      MailMessage message = new MailMessage(
                "[email protected]", 
                recipient, 
                "Test Email", 
                dt.ToString() 
                ); 
      SmtpClient client = new SmtpClient(mailServer); 
      var AuthenticationDetails = new NetworkCredential("[email protected]", "password"); 
      client.Credentials = AuthenticationDetails; 
      client.Send(message); 
     } 
     catch (Exception e) 
     { 

     } 

    } 
+0

có bàn phải trong cơ thể của email? – AllenG

+0

có, nội dung email là bảng – 14578446

+0

Bạn chỉ có thể viết ra html liệu phục vụ xây dựng một bảng, và đặt nó trong cơ thể email, ví dụ .:

tiêu đề
dữ liệu hàng
. Tuy nhiên, lưu ý rằng rất nhiều ứng dụng email đã tắt tính năng bật HTML, vì vậy hãy đảm bảo bạn không làm hỏng trải nghiệm người dùng. –

Trả lời

10

ok, hãy thử này ngay bây giờ:

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

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 = "[email protected]") 

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

    MailMessage message = new MailMessage("[email protected]", recipient); 
    message .IsBodyHtml = true; 
    message .Body = htmlString; 
    message .Subject = "Test Email"; 

    SmtpClient client = new SmtpClient(mailServer); 
    var AuthenticationDetails = new NetworkCredential("[email protected]", "password"); 
    client.Credentials = AuthenticationDetails; 
    client.Send(message); 
} 
catch (Exception e) 
{ 

} 

} 
1

Trước đây, tôi đã tạo một đối tượng EmailGrid.cs được kế thừa từ GridView. Sau đó, sử dụng một phương pháp như dưới đây để hiển thị HTML thành một chuỗi.

public string RenderControl() 
     { 
      StringBuilder stringBuilder = new StringBuilder(); 
      StringWriter stringWriter = new StringWriter(stringBuilder); 
      HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter); 
      RenderControl(htmlTextWriter); 

      return stringBuilder.ToString(); 
     } 
+0

Tôi đang làm điều này trong giao diện điều khiển ứng dụng, không có system.web.dll dere – 14578446

+0

là bạn gửi email thông qua giao diện điều khiển ứng dụng hoặc ứng dụng web theo cách bạn có thể thêm vào việc sử dụng ở đầu mã bằng cách sử dụng System.Web.Mail; – MethodMan

+0

nhưng tôi không thể tìm thấy tham chiếu đến dll system.web – 14578446

0

Nếu bạn muốn làm điều tương tự nhưng vòng lặp thông qua một DataAdapter xem liên kết này cho một ví dụ nhanh .. bởi vì bạn đang làm khá giống điều này ví dụ cho thấy với ngoại lệ bạn đang cố gắng để vượt qua toàn bộ DataTable vs xây dựng các kết quả vào cơ thể email .. How to use DataAdapter to DataTable via Email