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
Posted
Comments
LanFanNinja 26-Nov-11 15:51pm
   
See my solution below.

So you probably have code that looks something like this

try
{
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress("your_email_address@gmail.com");
    mail.To.Add("to_address");
    mail.Subject = "Test Mail - 1";
    mail.Body = "mail with attachment";

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

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

    SmtpServer.Send(mail);
    MessageBox.Show("mail Send");
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}


after the message is sent
SmtpServer.Send(mail);


you need to call
attachment.Dispose();

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