当附加到MailMessage对象时,C#为空文件

本文关键字:文件 对象 MailMessage | 更新日期: 2023-09-27 18:30:13

当我的应用程序崩溃时,我正试图发送一封电子邮件,附带了一个描述问题的文件(错误详细信息是从数据库中收集的)。我尝试过在不将文件附加到电子邮件的情况下创建该文件,它运行良好(从数据库收集数据)。这里有一个非常接近我的例子:

MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("Address1@test.com");
mailMessage.From = new MailAddress("Address2@test.com");
mailMessage.Subject = "Subject";
mailMessage.Body = "Body";
FileStream fs = new FileStream("Test.txt", FileMode.Create, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("Text");
Attachment attach = new Attachment(fs, "Test.txt", "Text/Plain");
mailMessage.Attachments.Add(attach);
SmtpClient smtp = new SmtpClient();
try
{
    smtp.Send(mailMessage);
}
catch(Exception ex)
{
     MessageBox.Show(ex.Message + Environment.NewLine + ex.InnerException);
}
sw.Close();

我也试过:

MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("Address1@test.com");
mailMessage.From = new MailAddress("Address2@test.com");
mailMessage.Subject = "Subject";
mailMessage.Body = "Body";
using (FileStream fs = new FileStream("Test.txt", FileMode.Create, FileAccess.ReadWrite))
{
     StreamWriter sw = new StreamWriter(fs);
     sw.WriteLine("Text");

     Attachment attach = new Attachment(fs, "Test.txt", "Text/Plain");
     mailMessage.Attachments.Add(attach);
     SmtpClient smtp = new SmtpClient();
     try
     {
         smtp.Send(mailMessage);
     }
     catch (Exception ex)
     {
          MessageBox.Show(ex.Message + Environment.NewLine + ex.InnerException);
     }
}

该文件已附加到电子邮件,具有一定的大小,但为空。我做错了什么?

提前谢谢。

当附加到MailMessage对象时,C#为空文件

回答我自己的问题。。。在这里找到答案。

这是我使用的代码:

MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("Address1@test.com");
mailMessage.From = new MailAddress("Address2@test.com");
mailMessage.Subject = "Subject";
mailMessage.Body = "Body";
using (MemoryStream memoryStream = new MemoryStream())
{
     byte[] contentAsBytes = Encoding.UTF8.GetBytes("Test");
     memoryStream.Write(contentAsBytes, 0, contentAsBytes.Length);
     memoryStream.Seek(0, SeekOrigin.Begin);
     ContentType contentType = new ContentType();
     contentType.MediaType = MediaTypeNames.Text.Plain;
     contentType.Name = "Test.txt";
      Attachment attach = new Attachment(memoryStream, contentType);
      mailMessage.Attachments.Add(attach);
      SmtpClient smtp = new SmtpClient();
      try
      {
          smtp.Send(mailMessage);
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message + Environment.NewLine + ex.InnerException);
      }
}

我使用了MemoryStream而不是FileStream

谢谢大家的帮助。

using (FileStream fs = new FileStream("Test.txt", FileMode.Create, FileAccess.ReadWrite))
{
    StreamWriter sw = new StreamWriter(fs);
    sw.WriteLine("Text");
    sw.Close();
}
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("Address1@test.com");
mailMessage.From = new MailAddress("Address2@test.com");
mailMessage.Subject = "Subject";
mailMessage.Body = "Body";

 Attachment attach = new Attachment("Test.txt", "Text/Plain"); //add a complete file path if needed
 mailMessage.Attachments.Add(attach);
 SmtpClient smtp = new SmtpClient();
 try
 {
     smtp.Send(mailMessage);
 }
 catch (Exception ex)
 {
      MessageBox.Show(ex.Message + Environment.NewLine + ex.InnerException);
 }

}