3

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("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 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 email, has a size, but is empty. What am I doing wrong?

Thanks in advance.

Zaehos
  • 175
  • 2
  • 13
  • try finishing writing the file before attaching it (close sw). – jac Nov 12 '14 at 18:06
  • did you check if your paths is correct, and please provide the error output that catch() generates – EugenSunic Nov 12 '14 at 18:07
  • 1
    Thank you for the comments. jac, The file cannot be closed before attached otherwise it crashes saying the file is closed. esunic, there is no error generated. The file is created without errors, but is empty. – Zaehos Nov 12 '14 at 18:18
  • Did you try `flush()` after you write to your `FileStream`? – Ckrempp Nov 12 '14 at 18:25
  • Yes, it does not work. – Zaehos Nov 12 '14 at 18:29
  • The best thing to do would be to use the "using" keyword for FileStream & StreamWriter, @Ckrempp, close() method will do the flush so this should not be the problem here, but maybe the using keyword will sort things out, try that... If that doesn't help then modify this line of code: Attachment attach = new Attachment(fs, "Test.txt", "Text/Plain"); I believe that this is the point where your code fails, check your parameters. – EugenSunic Nov 12 '14 at 18:29
  • @user1438403 jac told you to close the SW not the FS so that the content is actually written/stored in the FS. – Firen Nov 12 '14 at 18:31
  • I don't know if you can use the stream the way you are. Check this msdn page. http://msdn.microsoft.com/en-us/library/5ds708xx(v=vs.110).aspx – jac Nov 12 '14 at 18:32
  • 1
    If you do this `sw.Close()` before the `smtp.Send()` it should work. As @esunic said the `StreamWriter` performs the flush, but only when it closes. – Ckrempp Nov 12 '14 at 18:32
  • Edited the question with a using block. Still does not work. Also @Mucha M. It was the SW that I closed. – Zaehos Nov 12 '14 at 18:37
  • Did you include the smtp send inside the using? – EugenSunic Nov 12 '14 at 18:39
  • 1
    Your original code will work if you just put `sw.Close()` before the `smtp.Send()` – Ckrempp Nov 12 '14 at 18:40
  • I agree with @Ckrempp – EugenSunic Nov 12 '14 at 18:41
  • @esunic- I did try putting it before the smtp.Send() and I agree that it should work. But it is giving me an ObjectDisposedException for some reason. – Zaehos Nov 12 '14 at 18:48

2 Answers2

3

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 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.

Zaehos
  • 175
  • 2
  • 13
-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);
 }

}

jac
  • 9,666
  • 2
  • 34
  • 63