当我的应用程序崩溃并附带描述问题的附件时,我正在尝试发送电子邮件(错误详细信息是从数据库中收集的)。我已经尝试创建文件而不将其附加到电子邮件,它工作正常(使用从数据库收集的数据)。这是一个非常接近我的例子:
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);
}
}
该文件附在电子邮件上,有一个大小,但是为空。我做错了什么?
提前致谢。
答案 0 :(得分:3)
回答我自己的问题......找到答案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指定到Attachment Constructor中。
感谢大家的帮助。
答案 1 :(得分:-1)
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);
}
}