I am creating a csv file and attaching it to an email like so…
using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(csv)))
{
try
{
to = "email@email.com";
string from = "user@email.co.uk";
string subject = "Order p"+ OrderNumber;
string body = result;
SmtpClient SMTPServer = new SmtpClient("127.0.0.1");
MailMessage mailObj = new MailMessage(from, to, subject, body);
Attachment attachment = new Attachment(stream, new ContentType("text/csv"));
attachment.Name = "p" + OrderNo + "_" + CustomerReference+".csv";
mailObj.Attachments.Add(attachment);
SMTPServer.Send(mailObj);
}
catch (Exception ex)
{ }
}
This works fine, but how to i save the same csv file to a folder?thanks
The
File.WriteAllTextmethod in System.IO is probably the simplest way to accomplish this:where
<destination>is the path of the file to save the CSV to. This file will be created if it doesn’t exist yet, otherwise it will be overwritten.