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