Last post Oct 15, 2010 08:20 PM by sansan
Oct 15, 2010 05:05 PM|Chinni99|LINK
I have a ASP.Net website in which an email has to be sent to user with an excel attached if the user requests.
The user provides the email address, when click on a button to send report, an excel report with the result set should be attached to the email and sent to user.
The excel report will be saved on the server and through SMTP, I'm senidng the report in email. After the email is sent I should delete the excel file created on the server.When I'm trying to delete I get the error, the file is being used by SMTP object
Is there a way that I can delete this file after the email is sent? Please help me.
SMTP with C#
Oct 15, 2010 07:56 PM|Ahmed Moosa|LINK
did you tried the following ? :
protected void Page_Load(object sender, EventArgs e)
SmtpClient smtp = new SmtpClient();
smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
void smtp_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
File.Delete( " File Path " );
Hope this helps
Oct 15, 2010 08:20 PM|sansan|LINK
When I'm trying to delete I get the error, the file is being used by SMTP object (other process).
It will be helpful if you post the code that you are using. Most Likely, you might have opened a file stream before deleting it. Please close that file stream
FileStream fs = File.OpenRead("C:/test.txt"); //throws an error as the stream is openFile.Delete("C:/test.txt"); //this deletes file successfullyFileStream fs = File.OpenRead("C:/test.txt");//Do something with file streamfs.Close();//dispose resources which should release file handlefs.Dispose();File.Delete("C:/test.txt");