1
我試圖創建和發送動態csv文件作爲附件在郵件發送動態CSV文件作爲附件,但它不工作。我可以通過發送郵件發送郵件,但文件已經空了。以下是我的代碼:無法創建並通過郵件
var notProcessedRecords = FileFTPData.Where(x => 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 = "<div>Hello " + loggedInUserName + ", </br></br> Following item are restricted from processing, due to some errors. Please check Process description for the same. </br></br> From, </br>Streben Support </div>";
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)
{
}
有人可以幫我糾正這個問題嗎?
I set stream.position = 0,仍然文件在附件中爲空。 – Sachin
設置位置之前您已沖洗過嗎? –
號我沒有這些行之後沖洗:附件附接=新附件(流,新的ContentType(「文本/ CSV」)); attachment.Name = 「test.csv」; mail.Attachments.Add(附着); writer.Flush(); – Sachin