C#附加到MailMessage对象时为空文件
当我的应用程序崩溃时,我试图发送一封电子邮件,附带了一个描述问题的文件(错误详细信息是从数据库收集的)。我尝试过在不将文件附加到电子邮件的情况下创建该文件,但效果很好(使用从数据库收集的数据)。下面是一个非常接近我的例子:C#附加到MailMessage对象时为空文件,c#,email,smtp,attachment,C#,Email,Smtp,Attachment,当我的应用程序崩溃时,我试图发送一封电子邮件,附带了一个描述问题的文件(错误详细信息是从数据库收集的)。我尝试过在不将文件附加到电子邮件的情况下创建该文件,但效果很好(使用从数据库收集的数据)。下面是一个非常接近我的例子: MailMessage mailMessage = new MailMessage(); mailMessage.To.Add("Address1@test.com"); mailMessage.From = new MailAddress("Address2@test.co
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);
}
}
该文件已附加到电子邮件,大小为,但为空。我做错了什么
提前谢谢
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);
}
}回答我自己的问题。。。找到了答案 以下是我使用的代码:
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);
}
}
我使用了内存流而不是文件流。我还创建了一个内容类型对象,而不仅仅是在附件构造函数中指定MediaType
谢谢大家的帮助。在附加文件之前尝试完成文件的编写(关闭软件)。是否检查了路径是否正确,并请提供catch()生成的错误输出谢谢您的评论。jac,文件在附加之前不能关闭,否则它会崩溃,表明文件已关闭。esunic,没有生成错误。创建的文件没有错误,但为空。如果在
smtp.Send()
之前执行此操作,则应能正常工作。正如@esunic所说,StreamWriter
只在关闭时执行刷新。如果您只需将sw.Close()
放在smtp.Send()
之前,您的原始代码就可以工作了。谢谢,太好了!这导致我在一些代码中解决了同样的问题。如果你好奇,它是memoryStream.Seek(0,SeekOrigin.Begin)代码>这是解决问题的方法。