none
unlocking and deleting a file RRS feed

  • Question

  • Hi!

    I created an application having the following format:

     

     

    class clss

    {

    public clss()

    {

    file.create("c:\\txtfile.txt");

    SendViaEmail("c:\\txtfile.txt");

    file.delete("c:\\txtfile.txt");

    }

    }

     

     

    where the function SendViaEmail() will create all the objects required to attach the textfile to the email and send it.

    But file deletion is failing. If I comment out the function SendViaEmail(), the file will be deleted. Shouldn't the file get unlocked once the MailMessage object is destroyed upon leaving the function SendViaEmail() ?

    Any ideas?

     

     

    edit:

    I tried calling GC.collect() after the SendViaEmail() function call, but it didn't help.

    Friday, October 1, 2010 3:18 PM

Answers

  • Apparently the culprit is the MailMessage object. I wrongly assumed that the object will be automatically disposed when execution of the function terminates.  Now I just added a oMsg.dispose() call at the end, and now it works :)
    • Marked as answer by hhgauci Friday, October 1, 2010 4:09 PM
    Friday, October 1, 2010 4:09 PM

All replies

  • It seems like the SendViaEmail() is handing off the work to a background thread that is sending the mail. The background thread is still using the file when SendViaEmail() returns.


    What's the implementation of SendViaEmail()?

    Friday, October 1, 2010 3:29 PM
  • Thx Matthew for your reply. I've included the whole function here:

    private void SendViaEmail(POP3Handler.email email, List<string> Attachments, string emailAddress, string DisplayName)
            {
                MailMessage oMsg = new MailMessage();

                // Set the message sender
                oMsg.From = new MailAddress(emailAddress, DisplayName);

                // The .To property is a generic collection, 
                    oMsg.To.Add(new MailAddress(email.ReplyTo.Address, email.ReplyTo.DisplayName));

                // Set the content
                oMsg.Subject = email.Subject;
                oMsg.Body = "bla";
                oMsg.IsBodyHtml = true;

                foreach (string att in Attachments)
                {
                    oMsg.Attachments.Add(new Attachment(att));
                }

                
                SmtpClient oSmtp = new SmtpClient("smtp.gmail.com");

                oSmtp.Port = 587;
                oSmtp.EnableSsl = true;
                oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;

       //authenticate against the server.
                NetworkCredential oCredential = new NetworkCredential("username", "pass");
                oSmtp.UseDefaultCredentials = false;
                oSmtp.Credentials = oCredential;

                // Open certificate store of current user
                X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                store.Open(OpenFlags.ReadOnly);

                // Look for the certificate with specific subject           
                X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates.Find(X509FindType.FindBySubjectName, oMsg.From.DisplayName, true);

                if (storecollection.Count != 0)
                {
                    oSmtp.ClientCertificates.Add(storecollection[0]);
                }

                //Send it
                oSmtp.Send(oMsg);
            }
    Friday, October 1, 2010 3:43 PM
  • Did you call Dispose on the Attachment object?
    Friday, October 1, 2010 3:57 PM
  • Apparently the culprit is the MailMessage object. I wrongly assumed that the object will be automatically disposed when execution of the function terminates.  Now I just added a oMsg.dispose() call at the end, and now it works :)
    • Marked as answer by hhgauci Friday, October 1, 2010 4:09 PM
    Friday, October 1, 2010 4:09 PM