title: sending email with excel attachment using C# Console app
body: <p>`I am trying to sending email through C# console app. In this I want to attach excel file. That file will not be save any where directly send as attachment with email. Here i have also retrive data through datatable using store procdure.
  Note: here i have to use also App.config
  please help me with some code.</p>
  <p>here i have tried some code in program.cs</p>
  <pre><code> using System;
  using System.Configuration;
  using System.Data;
  using System.Data.SqlClient;
  using System.IO;
  using System.Net;
  using System.Net.Mail;
  using System.Net.Mime;
  using Excel = Microsoft.Office;
  
  using ClosedXML.Excel;
  using Microsoft.AspNetCore.Http;
  using OpenQA.Selenium.Remote;
  //using OpenQA.Selenium;
  
  namespace EmailConfiguration
  {
      class Program
      {
          public static void Main(string[] args)
          {
              // Console.WriteLine(&quot;Hello World!&quot;);
              //EmailNotification en = new EmailNotification();
              EmailNotification.SendMail(&quot;minaxi.singh@ajsas.com&quot;, &quot;Send a Mail&quot;, &quot;Sending email demo&quot;);
              Console.WriteLine(&quot;Mail Sent &quot;);
          }
          //created one static class
          public static class EmailNotification
          {
              //added four datatype
             const string SmtpServer = &quot;smtp.gmail.com&quot;;
              const int SmtpPort = 587;
              const string FromAddress = &quot;nqtest@nsa.com&quot;;
              const string Password = &quot;Neoqt@2022&quot;;
  
   
  
              //createing send mail method
              public static void SendMail(string toAddress, string Subject, string body)
              {
                  var client = new SmtpClient(SmtpServer, SmtpPort)
                  {
                      Credentials = new NetworkCredential(FromAddress, Password),
                      EnableSsl = true
                  };
                  MailMessage message = new MailMessage();
                  message.To.Add(&quot;minaxi.singh@neant.com&quot;);
  
  
  
  
  
                  //send mail to multiple person
                  foreach (var item in toAddress.Split(new[] { &quot;;&quot; }, StringSplitOptions.RemoveEmptyEntries))
                  {
                      message.To.Add(item);
                  }
                  message.From = new MailAddress(FromAddress);
                  message.Body = body;
                  message.Subject = Subject;
                 // DataTable dt = GetUserData();
                  GetUserData();
  
                  ExportToExcel(dt, &quot;E:\\Minaxi&quot;);
  
  
                  // Attachment attachment = new Attachment(&quot;GetExcelFile.c&quot;, MediaTypeNames.Application.Octet);
                  //message.Attachments.Add(attachment);
                  //GetExcelFiles _getExcelFiles = new GetExcelFiles();
                  // var str =  
  
                  try
                  {
                      client.Send(message);
                  }
                  catch (Exception e)
                  {
                      throw;
                  }
              }
              public static DataSet GetUserData()
              {
                  DataTable dt = new DataTable();
                  DataSet ds = new DataSet();
                  using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[&quot;iMixClaimConnection&quot;].ConnectionString))
                  {
                      try
                      {
                          SqlCommand cmd = new SqlCommand(&quot;usp_pg_AttachmentForMailing&quot;, con);
                          cmd.CommandType = CommandType.StoredProcedure;
                          SqlDataAdapter da = new SqlDataAdapter();
                          da.SelectCommand = cmd;
                          ds = new DataSet();
  
                          da.Fill(ds);
                          // dt = ds.Tables[0];
                       
  
                         
  
  
                      }
                      catch (Exception)
                      {
                          throw;
                      }
                      finally
                      {
                          ds.Dispose();
                      }
                  }
                  return ds;
              }
   public  void ExportToExcel()
                  {
                  DataSet ds = GetUserData();
                  using (XLWorkbook wb = new XLWorkbook())
                  {
                      wb.Worksheets.Add(ds);
                      wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                      wb.Style.Font.Bold = true;
  
                     HttpContext.Current.Response.Clear();
                      Response.Buffer = true;
                      Response.Charset = &quot;&quot;;
                      Response.ContentType = &quot;application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&quot;;
                      Response.AddHeader(&quot;content-disposition&quot;, &quot;attachment;filename=UserReport.xlsx&quot;);
  
                      using (MemoryStream MyMemoryStream = new MemoryStream())
                      {
                          wb.SaveAs(MyMemoryStream);
                          MyMemoryStream.WriteTo(Response.OutputStream);
  
                          Response.Flush();
                          Response.End();
                      }
                  }
              }
  </code></pre>