Sending An Email With Attachment Of Database Table Using C# Windows forms Application.

In this article I will show you how to send email with attachment (We can Send our full database table) in one click using Gmail SMTP Mail server.

I explained about how to send email with attachment (like Excel file)using Gmail SMTP Mail Server in c# windows forms application.

Step 1: Create a new project in Microsoft Visual Studio 2015 (File -> New -> Project -> Visual C# -> Windows Forms Application)Type Some Name: to Your Form then Click Ok Button.

Step 2: Now Go to Add Reference(Solution Explorer ->Reference -> Add Reference),
Select ClosedXml.dll , DocumentFormat.OpenXml.dll , MySql.Data.dll.

If You Don’t Have This In Your Reference Page DOWNLOAD this here in this link.

And We need OpenXMLSDv2.msi For sending mail Download this at here.

Step 3: Import the below namespaces – (just type them after ‘using System.Text’).

  1. using ClosedXML.Excel;
  2. using System.Net.Mail;
  3. using System.IO;
  4. using MySql.Data.MySqlClient;
After Complete this Now Our Coding is:
  1. using ClosedXML.Excel;
  2. using MySql.Data.MySqlClient;
  3. using System;
  4. using System.Data;
  5. using System.IO;
  6. using System.Net.Mail;
  7. using System.Windows.Forms;
  8. namespace SendingMail
  9. {
  10. public partial class Form1: Form
  11. {
  12. public Form1()
  13. {
  14. InitializeComponent();
  15. }
  16. //connection to our MySql Server
  17. String MyConString = "SERVER=******;" + "DATABASE=*****;" + "UID=root;" + "PASSWORD=pws;" + "Convert Zero Datetime = True";
  18. //send Button
  19. private void btnSend_Click(object sender, EventArgs e)
  20. {
  21. DataTable dt = GetData();
  22. dt.TableName = "data";
  23. using(XLWorkbook wb = new XLWorkbook())
  24. {
  25. wb.Worksheets.Add(dt);
  26. using(MemoryStream memoryStream = new MemoryStream())
  27. {
  28. wb.SaveAs(memoryStream);
  29. byte[] bytes = memoryStream.ToArray();
  30. memoryStream.Close();
  31. using(MailMessage Msg = new MailMessage())
  32. {
  33. Msg.From = new MailAddress("YOUR MAIL ID .com");
  34. Msg.To.Add(txtTo.Text);
  35. Msg.Subject = "DATA Exported Excel";
  36. Msg.Body = "DATA Exported Excel Attachment";
  37. Msg.Attachments.Add(new Attachment(new MemoryStream(bytes), "DATA.xlsx"));
  38. Msg.IsBodyHtml = true;
  39. SmtpClient smtp = new SmtpClient();
  40. smtp.Host = "smtp.gmail.com";
  41. smtp.EnableSsl = true;
  42. System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
  43. credentials.UserName = "YOUR MAIL ID . com";
  44. credentials.Password = "YOUR MAIL ID PASSWORD";
  45. smtp.UseDefaultCredentials = true;
  46. smtp.Credentials = credentials;
  47. smtp.Port = 587;
  48. smtp.Send(Msg);
  49. MessageBox.Show("MAIL SENT SUCCESSFULLY TO " + txtTo.Text);
  50. txtTo.Text = "";
  51. }
  52. }
  53. }
  54. }
  55. private DataTable GetData()
  56. {
  57. using(MySqlConnection conn = new MySqlConnection(MyConString))
  58. {
  59. using(MySqlCommand cmd = new MySqlCommand("SELECT * from TABLE NAME;", conn))
  60. using(MySqlDataAdapter sda = new MySqlDataAdapter())
  61. {
  62. cmd.Connection = conn;
  63. sda.SelectCommand = cmd;
  64. using(DataTable dt = new DataTable())
  65. {
  66. sda.Fill(dt);
  67. return dt;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }