Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a table in email

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

        }

    }
like image 290
14578446 Avatar asked Jan 10 '12 22:01

14578446


People also ask

Can you put a table in a Gmail email?

A common feature people miss when transitioning to Gmail is the ability to insert a table into an email. However, although Gmail does not have an in-built function to insert a table, it does support the use of tables in a message.

How do I insert a table into a email template?

Create a table. Select the location where you want to add the table. Select Insert > Table, and select the number of columns and rows you want. Add text to the table.


1 Answers

ok, try this now:

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)
 {

 }

}
like image 134
user1157131 Avatar answered Sep 27 '22 18:09

user1157131