Tuesday, July 23, 2013

How To- Send DataGridView Data in Email in Window Form Application

In my one post I explained you how to Send Grdiview Data in Email. Here I am going to show you how to send DataGridview content in E-mail in window application.

You can also find my other articles related to C#ASP.Net jQuery, Java Script and SQL Server.

First take a look on the below image.
DataGridView with Data

In the above image, we have a DataGridView with some data and a button with text "Send Mail". Our requirement is that we have to send the data of DataGridView in mail in tabular form.

For sending the DataGridView content in mail we have to write the following code on Click Event of button.
C# Code

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //Create instance of MailMessage Class
                MailMessage mail = new MailMessage();
                //Assign From mail address
                mail.From = new MailAddress("frommail@gmail.com");
                //Set To mail address
                mail.To.Add(new MailAddress("tomaiil@gmail.com"));
                //Set Subject of mail
                mail.Subject = "Send DataGridView Data in Mail";
                //Create Mail Body
                mail.Body = "Please check the following data 
";
                mail.Body += GetTableFromDataGrid();
                //for sending body as HTML
                mail.IsBodyHtml = true;
                //Create Instance of SMTP Class
                SmtpClient SmtpServer = new SmtpClient();
                //Assign Host
                SmtpServer.Host = "smtp.gmail.com";
                //Assign Post Number
                SmtpServer.Port = 587;
                //Setting the credential for authentiicate the sender
                SmtpServer.Credentials = new System.Net.NetworkCredential("mymail@gmail.com", "password");
                //Enable teh Secure Soket Layer to Encrypte the connection 
                SmtpServer.EnableSsl = true;
                //Sending the message
                SmtpServer.Send(mail);
                MessageBox.Show("Mail Sends Successfully!!");                
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }  
        }
In the above code, I have called a function named GetTableFromDataGrid(), which is as follows-
public string GetTableFromDataGrid()
        {
            StringBuilder strTable = new StringBuilder();
            try
            {
                strTable.Append("<table border='1' cellpadding='0' cellspacing='0'>");
                strTable.Append("<tr>");
                //Create Header Row for Table
                foreach (DataGridViewColumn col in dataGridView1.Columns)
                {
                    strTable.AppendFormat("<th>{0}</th>", col.HeaderText);
                }
                strTable.Append("</tr>");
                //Create Table Rows
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {                    
                    strTable.Append("<tr>");
                    foreach (DataGridViewCell cell in dataGridView1.Rows[i].Cells)
                    {
                        if (cell.Value != null)
                        {
                            strTable.AppendFormat("<td>{0}</td>", cell.Value.ToString());
                        }
                    }
                    strTable.Append("</tr>");
                }
                strTable.Append("</table>");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }           
            return strTable.ToString();
        }
Output Mail
I hope this article will be helpful for you. I would like to have any feedback from you. Your valuable feedback, question, or comments about this article are always welcome.

No comments:

Post a Comment

^ Scroll to Top