C # Empty file when connecting to MailMessage object

I am trying to send an email when my application crashes with an attachment describing the problem (the error details are collected from the database). I tried to create a file without tying it to email and it works fine (with data collected from the database). Here's an example very close to what I have:

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

      

I've also tried:

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

      

The file is attached to the letter, has a size, but is empty. What am I doing wrong?

Thanks in advance.

+3


source to share


2 answers


Answering my own question ... Found the answer here .

Here is the code I used:

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

      



I used MemoryStream instead of FileStream . I also created a content type object instead of just specifying the MediaType in the attachment constructor.

Thanks for helping everyone.

+3


source


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

      



}

0


source







All Articles