title: How to create a csv and attach to email and send in c#
body: <p>This is how i am currently creating a table and sending it in an email. What i would like to do is instead of creating a table and sending it as text in the email i would like to create a csv file and attach it to this email and then send this. could someone please help show me how this can be done? thanks</p>
  
  <pre><code>using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(csv)))
      {
          try
          {
              string to = "";
              string from = "";
              string subject = "Order";
              string body = sb.ToString();
              SmtpClient SMTPServer = new SmtpClient("127.0.0.1");
              MailMessage mailObj = new MailMessage(from, to, subject, body); 
              mailObj.Attachments.Add(new Attachment(stream, new ContentType("text/csv")));
              mailObj.IsBodyHtml = true;
              SMTPServer.Send(mailObj);
          }
          catch (Exception ex)
          { return "{\"Error\":\"Not Sent\"}"; }
      }
  </code></pre>