1

I am using EXCEL interop for genearting Excel and then i am attaching it as an attachment to mail and then send the mail.After mail send i want to delete generated file. On deleting it is throwing error:

this file cannot be deleted as it is is used by another process

I searched on SO and other sites and found interesting facts about COM Components.
reference:- How do I properly clean up Excel interop objects?

if i am not attaching it as attachment then file is getting deleted. Before deletion I am removing all COM references.but when i am using it as attachments it is throwing error: my code is like :

    workbook.SaveAs(root + statics + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                    // Garbage collecting
                    // Clean up references to all COM objects
                    // As per above, you're just using a Workbook and Excel Application instance, so release them:
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    Marshal.FinalReleaseComObject(m_objRange);

                    Marshal.FinalReleaseComObject(worksheet);
                    workbook.Close(false, Type.Missing, Type.Missing);
                    Marshal.FinalReleaseComObject(workbook);
                    app.Quit();
                    Marshal.FinalReleaseComObject(app);

                    MailMessage mm = new MailMessage("def@gmail.com", "xyz@gmail.com", "TestMsg", "Hi");
                    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                    client.Credentials = CredentialCache.DefaultNetworkCredentials;

                    //Commenting Below two lines works fine ....!!!!!!!!!....WHY..???

                    //Attachment data = new Attachment(root + statics + ".xls");
                    //mm.Attachments.Add(data);

                    client.Send(mm);

                    File.Delete(root + statics + ".xls");

how to remove references after attaching it to mail. Thanks

Community
  • 1
  • 1
Pranav
  • 8,563
  • 4
  • 26
  • 42

2 Answers2

1

Just Adding data.Dispose() after sending mail is working fine for me.here is modified code:

workbook.SaveAs(root + statics + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                    // Garbage collecting
                    // Clean up references to all COM objects
                    // As per above, you're just using a Workbook and Excel Application instance, so release them:
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    Marshal.FinalReleaseComObject(m_objRange);

                    Marshal.FinalReleaseComObject(worksheet);
                    workbook.Close(false, Type.Missing, Type.Missing);
                    Marshal.FinalReleaseComObject(workbook);
                    app.Quit();
                    Marshal.FinalReleaseComObject(app);

                    MailMessage mm = new MailMessage("def@gmail.com", "xyz@gmail.com", "TestMsg", "Hi");
                    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                    client.Credentials = CredentialCache.DefaultNetworkCredentials;


                    Attachment data = new Attachment(root + statics + ".xls");
                    mm.Attachments.Add(data);
                    //Now Working Fine:-
                     data.Dispose();
                    client.Send(mm);

                    File.Delete(root + statics + ".xls");
Pranav
  • 8,563
  • 4
  • 26
  • 42
  • Ha - we posted at roughly the same time, but you have indeed solved your own problem. +1. – dash Mar 01 '12 at 13:08
1

The MailMessage and SmtpClient should be disposed, doing that, the MailMessage, also disposes any attachments.

using(SmtpClient client = new SmtpClient("smtp.gmail.com", 587))
using(MailMessage mm = new MailMessage("def@gmail.com", "xyz@gmail.com", "TestMsg", "Hi"))
{ 

    client.Credentials = CredentialCache.DefaultNetworkCredentials; 
    client.Send(mm); 
}
Steve
  • 213,761
  • 22
  • 232
  • 286