2012-01-10 11 views
5

मुझे ईमेल में एक प्रश्न के परिणाम भेजने की आवश्यकता है।ईमेल में एक तालिका भेजें

GetDataTable():: मैं दो तरीकों का उपयोग कर रहा क्वेरी निष्पादित और datatable प्राप्त करने के लिए (जो ईमेल में भेजे जाने की आवश्यकता)

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 = "[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

तालिका ईमेल के मुख्य भाग में होना जरूरी है? – AllenG

+0

हाँ, ईमेल की सामग्री तालिका – 14578446

+0

तुम सिर्फ एक मेज के निर्माण के लिए कच्चे एचटीएमएल बाहर लिख सकते हैं, और ईमेल के शरीर में डाल दिया, उदा .:

हैडर
पंक्ति डेटा
है। ध्यान दें, हालांकि, बहुत से ईमेल क्लाइंटों में HTML- सक्षम मेल बंद है, इसलिए सुनिश्चित करें कि आप उपयोगकर्ता अनुभव को तोड़ नहीं सकते हैं। –

उत्तर

10

ठीक है, अब इस प्रयास करें:

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

अतीत में के स्थान पर एक स्ट्रिंग के लिए ठीक काम करता है, मैं एक वस्तु EmailGrid.cs जो GridView से विरासत कर दिया है। फिर HTML को एक स्ट्रिंग में प्रस्तुत करने के लिए नीचे की तरह एक विधि का उपयोग किया।

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

      return stringBuilder.ToString(); 
     } 
+0

मैं इसे कंसोल एप्लिकेशन में कर रहा हूं, system.web.dll dere – 14578446

+0

नहीं है क्या आप कंसोल ऐप या वेब ऐप के माध्यम से ईमेल भेज रहे हैं या तो आप System.Web.Mail का उपयोग कर कोड के शीर्ष पर उपयोग करने के लिए जोड़ सकते हैं; – MethodMan

+0

लेकिन मैं system.web dll को संदर्भ नहीं ढूंढ पा रहा हूं – 14578446

0

आप एक त्वरित उदाहरण .. के लिए इस लिंक पर एक नज़र DataAdapter के माध्यम से एक ही बात है, लेकिन पाश datatable के माध्यम से करना चाहते हैं, तो इसलिए कि आप काफी एक ही बात इस उदाहरण अपवाद के साथ चलता कर रहे हैं आप पूरे datatable ईमेल शरीर में परिणाम के निर्माण बनाम पारित करने के लिए कोशिश कर रहे हैं .. How to use DataAdapter to DataTable via Email

संबंधित मुद्दे