Click here to Skip to main content
14,696,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have files stored in sql database
& i need to send those files throught email as attachment
so i create this file on specific folder
then i send as attachment with email
when i try to delete those files after sending emails
i have this is error

the file can't be deleted because it's used by another process

so how i can remove those files from being used
to be deleted
with regards
LanFanNinja 26-Nov-11 15:51pm
See my solution below.

So you probably have code that looks something like this

    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("");
    mail.From = new MailAddress("");
    mail.Subject = "Test Mail - 1";
    mail.Body = "mail with attachment";

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("you attachment file");

    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
    SmtpServer.EnableSsl = true;

    MessageBox.Show("mail Send");
catch (Exception ex)

after the message is sent

you need to call

to free the file up for deleting.

Hope this helps.
With out code how can we give you exact solution, check the possible case from the below threads

File being used by another process after[^]
Cant Access File because it is being used by another process[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900