Zaehos
Zaehos

Reputation: 185

C# Empty File When Attached to MailMessage Object

I am trying to send an email when my application crashes with an attached file describing the issue (Error details is gathered from a database). I've tried creating the file without attaching it to an email and it works fine (with data gathered from a database). Here is an example very close to what I have :

MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("[email protected]");
mailMessage.From = new MailAddress("[email protected]");
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 also tried :

MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("[email protected]");
mailMessage.From = new MailAddress("[email protected]");
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 email, has a size, but is empty. What am I doing wrong?

Thanks in advance.

Upvotes: 3

Views: 4452

Answers (2)

Zaehos
Zaehos

Reputation: 185

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

Here is the code I used :

MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("[email protected]");
mailMessage.From = new MailAddress("[email protected]");
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 a MemoryStream instead of a FileStream. I also created a content type object instead of just specifying the MediaType into the Attachment Constructor.

Thank you for the help everyone.

Upvotes: 3

jac
jac

Reputation: 9726

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("[email protected]");
mailMessage.From = new MailAddress("[email protected]");
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);
 }

}

Upvotes: -1

Related Questions