我正在尝试将日志文件附加到我的邮件中,但我没有收到任何内容。这是我的例子。我做错了什么?我可以看到我的文件 log.txt 已创建并放置在路径中,但如果我尝试附加此部分,它不会发送电子邮件。
private void AssignAttachment(MailMessage msg, DataTable dt, StreamReader streamreader)
{
FileStream fs = (FileStream)streamreader.BaseStream;
string file = "c:\\temp\\log.txt";
using (StreamWriter sw = new StreamWriter(file, false, Encoding.GetEncoding("UTF-8")))
{
for (int i = 0; i < dt.Rows.Count; i++)
{
//Testing. Col 0 = string results
sw.WriteLine(dt.Rows[i][0]);
}
}
MemoryStream ms = new MemoryStream();
using (FileStream fileStream = File.OpenRead(file))
{
ms.SetLength(fileStream.Length);
fileStream.Read(ms.GetBuffer(), 0, (int)fileStream.Length);
}
//MailMessage msg = new MailMessage();
//msg is well-configured email. Sends emails if I don't attach anything
msg.Attachments.Add(new Attachment(ms,file, System.Net.Mime.MediaTypeNames.Text.Plain);
}