【问题标题】:C# Empty File When Attached to MailMessage Object附加到 MailMessage 对象时的 C# 空文件
【发布时间】:2014-11-12 17:43:58
【问题描述】:

当我的应用程序崩溃并附带描述问题的附件时,我正在尝试发送电子邮件(从数据库收集错误详细信息)。我尝试在不将其附加到电子邮件的情况下创建文件,并且它工作正常(使用从数据库收集的数据)。这是一个非常接近我所拥有的示例:

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);
     }
}

文件附加到电子邮件中,有大小,但为空。我做错了什么?

提前致谢。

【问题讨论】:

  • 在附加文件之前尝试完成写入文件(关闭 sw)。
  • 你检查你的路径是否正确,请提供catch()产生的错误输出
  • 感谢 cmets。 jac,在附加之前无法关闭文件,否则会崩溃,说明文件已关闭。 esunic,没有产生错误。该文件创建时没有错误,但为空。
  • 如果你在smtp.Send() 之前这样做sw.Close() 它应该可以工作。正如@esunic 所说,StreamWriter 执行刷新,但仅在关闭时执行。
  • 如果您将sw.Close() 放在smtp.Send() 之前,您的原始代码将起作用

标签: c# email smtp attachment


【解决方案1】:

回答我自己的问题...找到答案here

这是我使用的代码:

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。我还创建了一个内容类型对象,而不是仅将 MediaType 指定到附件构造函数中。

感谢大家的帮助。

【讨论】:

  • 谢谢,太好了!
  • 这导致我在一些代码中解决了同样的问题。如果您好奇,memoryStream.Seek(0, SeekOrigin.Begin); 正在解决问题。
【解决方案2】:
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);
 }

}

【讨论】:

    猜你喜欢
    • 2011-07-17
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 2011-05-15
    • 2015-10-19
    相关资源
    最近更新 更多