ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Send email with dynamic generated excel as attachment using C# in ASP.Net<p>Hi narasiman,</p> <p>Change your code with the below code.</p> <pre class="brush: csharp">int count = 0; string connectionstring = &#34;Server=(local);initial catalog=Test;Trusted_Connection=True&#34;; SqlConnection sqlConnection = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand(); SqlDataReader reader; DataSet ds = new DataSet(); cmd.CommandText = &#34;select * from Empdetails&#34;; cmd.CommandText += &#34; where shifttype = @par &#34;; cmd.Parameters.Add(&#34;@par&#34;, SqlDbType.Int).Value = j; cmd.CommandType = CommandType.Text; cmd.Connection = sqlConnection; sqlConnection.Open(); reader = cmd.ExecuteReader(); string filePath = @&#34;C:\Users\God\Desktop\DataDump\&#34; + j + &#34;Excel.xls&#34;; System.IO.StreamWriter sw_In = new System.IO.StreamWriter(filePath); if (reader.HasRows) { while (reader.Read()) { if (count == 0) { for (int i = 0; i &lt; reader.FieldCount; i++) { sw_In.AutoFlush = true; sw_In.Write(reader.GetName(i) + &#34;\t&#34;); } sw_In.Write(&#34;\n&#34;); count = 1; } for (int i = 0; i &lt; reader.FieldCount; i++) { sw_In.AutoFlush = true; sw_In.Write(reader[i].ToString() + &#34;\t&#34;); } sw_In.Write(&#34;\n&#34;); } sw_In.Dispose(); } reader.Close(); sqlConnection.Close(); MailMessage mis = new MailMessage(); mis.From = new MailAddress(&#34;rajesh@gmail.com&#34;, &#34;Report&#34;); mis.IsBodyHtml = true; mis.To.Add(&#34;rajesh@gmail.com&#34;); mis.CC.Add(new MailAddress(&#34;rajesh@gmail.com&#34;)); mis.Subject = &#34;Data Dump Report&#34;; mis.Attachments.Add(new Attachment(filePath, &#34;application/vnd.ms-excel&#34;)); SmtpClient smtpserver = new SmtpClient(&#34;smtp.gmail.com&#34;); smtpserver.Credentials = new System.Net.NetworkCredential(&#34;rajesh@gmail.com&#34;, &#34;12345&#34;); smtpserver.Host = &#34;smtp.gmail.com&#34;; smtpserver.Port = 587; smtpserver.EnableSsl = true; smtpserver.Send(mis);</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/581809/Send-email-with-dynamic-generated-excel-as-attachment-using-C-in-ASPNet/https://www.aspforums.net:443/Threads/581809/Send-email-with-dynamic-generated-excel-as-attachment-using-C-in-ASPNet/Sun, 28 Aug 2016 05:40:55 GMT