I'm writing something that will create a .csv file in memory and email it as an attachment. The code below successfully emails a .csv file, but it is empty. I'm sure I'm missing something simple...
MemoryStream memoryStream = new MemoryStream();
TextWriter tw = new StreamWriter(memoryStream);
tw.WriteLine("test,hello");
tw.WriteLine("1234,543");
Attachment attachment = new Attachment(memoryStream, new ContentType("text/csv"));
attachment.Name = "test.csv";
var Smtp = new SmtpClient();
Smtp.UseDefaultCredentials = false;
var NetworkCredentials = new NetworkCredential() { UserName = "NOPE@gmail.com", Password = "NO" };
Smtp.Port = 587;
Smtp.EnableSsl = true;
Smtp.Host = "smtp.gmail.com";
Smtp.Credentials = NetworkCredentials;
MailMessage msg = new MailMessage();
msg.From = new MailAddress("NAY@gmail.com");
msg.To.Add("X@gmail.com");
msg.Subject = "subject text";
msg.Body = "Attached is a file.";
msg.Attachments.Add(attachment);
Smtp.Send(msg);