We help IT Professionals succeed at work.

How to delete files which are sent through mail attachment programmatically in C# code?

Minesh Shah
Minesh Shah asked
on
Hi, I write a code to create programmatically excel sheet 2007 and write something and save as on C drive and then send it through mail attachment and then want to delete programmatically,
Below is the code sample:
               string Path = @"c:\Original Log Report.xlsx";
                Excel.Application xlApp;
                Excel.Workbook xlWorkBook;
                Excel.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;
                xlApp = new Excel.ApplicationClass();
                xlWorkBook = xlApp.Workbooks.Open(Path, true, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                int first_row = 2;
                int fisrt_column = 1;
xlWorkSheet.Cells[first_row, fisrt_column] = "Hello";
                xlWorkSheet.Cells[first_row, (fisrt_column + 1)] = "Testing";

                xlWorkBook.SaveAs(@"c:\Log Report.xlsx", misValue, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();

                releaseObject(xlWorkSheet);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);
//----sending mail attachment------------
SmtpClient SmtpServer = new SmtpClient("myworld.com");
            MailMessage mail = new MailMessage();
            mail.IsBodyHtml = true;
            mail.From = new MailAddress("abc@world.com");
            mail.To.Add("abc@world.com");
mail.Subject = "Log Of Pen Drive";
mail.Body = "Please find attched Log Of Pen Drive";
            System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(@"c:\Log Report.xlsx");
            mail.Attachments.Add(attachment);
            SmtpServer.Port = 25;
            SmtpServer.Credentials = new System.Net.NetworkCredential("abc", "password");
            SmtpServer.Send(mail);
            File.Delete(@"c:\Log Report.xlsx"); /* here error is coming that cannot delete log Report:It is being used by another persone or programme.*/
           
Label2.Text = "Mail has been sent.";
private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            Label1.Text = ex.ToString();
        }
        finally
        {
            GC.Collect();
        }
    }
How to resolve it?
Error: “System.IO.IOException: The process cannot access the file 'c:\Log Report.xlsx' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.Delete(String path) at _Default.Page_Load(Object sender, EventArgs e)”
And one more thing after execution of the code EXCEL.EXE process is created how to stop it?
Comment
Watch Question

Kiran SonawaneProject Lead
Top Expert 2011

Commented:
Try like this

SmtpServer.Send(mail);
smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);



void smtp_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        File.Delete(@"c:\Log Report.xlsx");
    }
Dispose or release SmtpServer after successful sending Email


Dani
Minesh ShahSharePoint & all about it

Author

Commented:
Dear sonawanekiran, i tried your code but not working.
Dear mcs0506, i use mail.Dispose() and it's working fine now able to delete the file.

BUT what about EXCEL.EXE process, i want to stop/kill this process after execution of code by C# code.
Kindly help.
Minesh ShahSharePoint & all about it

Author

Commented:
thanks