
Introduction
In my work, it is very often required to generate some reports based on database contents. I use the Windows Forms DataGridView and print its contents, but here is a question - how can I send emails with the DataGridView contents? I can send emails with DataGridView contents in two ways - directly in the message body or as an attachment (in my example, as an HTML file).
Using the code
First of all, let's load data into the DataGridView. In this example, I use an XML file as a data source. I've placed it in the bin/Debug folder. I then create an HTML file from the DataGridView. For this, I have a StringBuilder with the DataGridView contents in it.
After that, I can either put it in the message body or in the attachment.
DataSet ds = new DataSet();
ds.ReadXml("dataFile.xml");dataGridView1.DataSource = ds;
dataGridView1.DataMember = "authors";
private StringBuilder htmlMessageBody(DataGridView dg)
{
StringBuilder strB = new StringBuilder();
strB.AppendLine("<html><body><center><" +
"table border='1' cellpadding='0' cellspacing='0'>");
strB.AppendLine("<tr>");
for (int i = 0; i < dg.Columns.Count; i++)
{
strB.AppendLine("<td align='center' valign='middle'>" +
dg.Columns[i].HeaderText + "</td>");
}
strB.AppendLine("<tr>");
for (int i = 0; i < dg.Rows.Count; i++)
{
strB.AppendLine("<tr>");
foreach (DataGridViewCell dgvc in dg.Rows[i].Cells)
{
strB.AppendLine("<td align='center' valign='middle'>" +
dgvc.Value.ToString() + "</td>");
}
strB.AppendLine("</tr>");
}
strB.AppendLine("</table></center></body></html>");
return strB;}
MailMessage myMessage = new MailMessage();
try
{
myMessage.From = "from@yourDomain.com"; myMessage.To = "to@recipientsDomain"; myMessage.Cc = "cc@someDomain.com"; myMessage.BodyEncoding = Encoding.UTF8;
myMessage.BodyFormat = MailFormat.Html;
myMessage.Body = htmlMessageBody(dataGridView1).ToString();
myMessage.Subject = "message subject";
myMessage.Fields.Add("http://schemas.microsoft.com/cdo/" +
"configuration/smtpauthenticate",
"1"); myMessage.Fields.Add("http://schemas.microsoft.com/cdo/" +
"configuration/sendusername",
"userName"); myMessage.Fields.Add("http://schemas.microsoft.com/cdo/" +
"configuration/sendpassword",
"password"); SmtpMail.SmtpServer = "your SMTP server";
SmtpMail.Send(myMessage);
MessageBox.Show("Message sent!");
}
catch (Exception ex)
{
MessageBox.Show("Error! "+ex.Message);
}
MailMessage myMessage = new MailMessage();
try
{
myMessage.From = "from@yourDomain.com"; myMessage.To = "to@recipientsDomain"; myMessage.Cc = "cc@someDomain.com"; myMessage.BodyEncoding = Encoding.UTF8;
myMessage.BodyFormat = MailFormat.Html;
myMessage.Body = "some body text"; myMessage.Subject = "message subject"; StreamWriter sw = new StreamWriter("file.html",
true, Encoding.UTF8); sw.Write(htmlMessageBody(dataGridView1).ToString());
sw.Close();
MailAttachment myAttach = new MailAttachment("file.html");
myMessage.Attachments.Add(myAttach);
myMessage.Fields.Add("http://schemas.microsoft.com/cdo/" +
"configuration/smtpauthenticate",
"1"); myMessage.Fields.Add("http://schemas.microsoft.com/cdo/" +
"configuration/sendusername",
"userName"); myMessage.Fields.Add("http://schemas.microsoft.com/cdo/" +
"configuration/sendpassword",
"password"); SmtpMail.SmtpServer = "your SMTP server";
SmtpMail.Send(myMessage);
MessageBox.Show("Message sent!");
}
catch (Exception ex)
{
MessageBox.Show("Error! " + ex.Message);
}
Points of Interest
Now, I do not need to export my DataGridView into a Word or Excel file and then send it to my chief - I can directly send the DataGridView to him :-)