title: Unable to create and send dynamic CSV file as attachment over mail
body: <p>I am trying to create and send dynamic csv file as attachment over mail, but it is not working. I am able to send mail with attachement, but file is going as empty. Below is my code:</p>
  
  <pre><code>var notProcessedRecords = FileFTPData.Where(x =&gt; x.LastName.StartsWith("9999")).ToList();
  
        string loggedInUserName = "Administrator";
  
        try
        {
        MemoryStream stream = new MemoryStream();
        TextWriter writer = new StreamWriter(stream, Encoding.Default);
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat(string.Format("{0}, {1}, {2}, {3}, {4}, {5} ", "FIRSTNAME", "LASTNAME", "USERID", "COMPANYNAME", "EMAIL", "PHONE"));
  
        foreach (var item in notProcessedRecords)
        {
            sb.AppendLine();
            sb.AppendFormat(string.Format("{0}, {1}, {2}, {3}, {4}, {5} ", item.FirstName, item.LastName, item.UserId, item.CompanyName, item.Email, item.Phone));
        }
        writer.WriteLine(sb);
  
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        mail.From = new MailAddress("******@gmail.com");
        mail.To.Add("*******@yahoo.com");
        mail.Subject = "FTP file processing status";
        mail.Body = "&lt;div&gt;Hello " + loggedInUserName + ", &lt;/br&gt;&lt;/br&gt; Following item are restricted from processing, due to some errors. Please check Process description for the same. &lt;/br&gt;&lt;/br&gt;  From, &lt;/br&gt;Streben Support &lt;/div&gt;";
        mail.IsBodyHtml = true;
  
        Attachment attachment = new Attachment(stream, new ContentType("text/csv"));
        attachment.Name = "test.csv";
        mail.Attachments.Add(attachment);
  
        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("******@gmail.com", "******");
        SmtpServer.EnableSsl = true;
  
        SmtpServer.Send(mail);
  
        }
        catch(Exception ex)
        {
        }
  </code></pre>
  
  <p>Can anybody help me in correcting the issue?</p>