locked
Mail Attachment Problem RRS feed

  • Question

  • I have a windows service that monitors another application. When the application gets closed it sends an email with the ErrorLogs attached. This works great however When IU try to fire back up the application it monitors I get an error saying the ErrorLog file is in use. The application checks to see the existence of the ErrorLog when it starts.

    Here is the email code

    MailMessage mail = new MailMessage(Properties.Settings.Default.fromAddress, Properties.Settings.Default.toAddress); 
                        mail.Subject = Properties.Settings.Default.subject; 
                        mail.Body = Properties.Settings.Default.body; 
                        Attachment attachment = new Attachment(Properties.Settings.Default.attachment); 
                        mail.Attachments.Add(attachment); 
                        SmtpClient client = new SmtpClient(Properties.Settings.Default.smtpServer); 
                        client.Credentials = new NetworkCredential(Properties.Settings.Default.fromAddress, Properties.Settings.Default.password); 
                        client.Port = Properties.Settings.Default.port; 
                        client.Send(mail); 
                        attachment.Dispose(); 
                        mail.Dispose(); 

    I thought that the dispose calls at the end would remedy this problem but it hasn't.

    Any ideas on this?
    Thursday, October 2, 2008 4:59 PM

Answers

  • Hmmm...

    Try using a MemoryStream in conjunction with File.ReadAllBytes(Properties.Settings.Default.attachment):

    using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(Properties.Settings.Default.attachment)))
    {
        Attachment attachment = new Attachment(ms);
    }
    David Morton - http://blog.davemorton.net/
    • Marked as answer by forsberg Thursday, October 2, 2008 5:54 PM
    Thursday, October 2, 2008 5:42 PM

All replies

  • Where is this ErrorLog file called?  I only see a method for sending an email, but nothing about the ErrorLog file in your code.
    David Morton - http://blog.davemorton.net/
    Thursday, October 2, 2008 5:16 PM
  • The line:

    Attachment attachment = new Attachment(Properties.Settings.Default.attachment);

    Properties.Settings.Default.attachment is a string that holds the file path to the ErrorLog.
    I know this work becuase I've recieved the email with the log attached.

    I would think that when I do attahcment.Dispose() it would free up the error log file.
    The mail has already been sent I don't see why it holds control over the file in the first place.
    Thursday, October 2, 2008 5:21 PM
  • Try a "using" block instead of calling dispose directly.  If your mail sending procedure fails, you'll never dispose of your stream or attachments.  If you use a using statement, this will be taken care of for you.  Also, ensure you're not opening the error log in another program.
    David Morton - http://blog.davemorton.net/
    Thursday, October 2, 2008 5:27 PM
  • Still does the same thing even with using blocks.

    Any idea on why adding a file to an email as an attachment will hold that file open?

    I do get the email from the service saying it closed. So the email is going out successfully.
    Thursday, October 2, 2008 5:39 PM
  • Hmmm...

    Try using a MemoryStream in conjunction with File.ReadAllBytes(Properties.Settings.Default.attachment):

    using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(Properties.Settings.Default.attachment)))
    {
        Attachment attachment = new Attachment(ms);
    }
    David Morton - http://blog.davemorton.net/
    • Marked as answer by forsberg Thursday, October 2, 2008 5:54 PM
    Thursday, October 2, 2008 5:42 PM
  • You my friend are a genius.

    Is that a problem with the framework by any chance?

    I had tried searching for the problem and it didn't seem to come up anywhere else.
    Thursday, October 2, 2008 5:54 PM