[RESOLVED] Attach Files to an E-Mail and Delete them
Hi all,
I am having a hard time deleting some files that I am attaching to an E-mail. After I send them, I want to archive them and delete them. The archive goes fine, but as soon as I try to delete them, I get an error stating "The file is in use by another process". I already tried by clearing the array I use to attach the files, but it does not help. This is what I am doing.
1. Create an array of file names within the directory and send it to a class, which is responsible for sending the E-mail with the attachments.
2. After the E-mail is sent, control is sent to the main code and I loop trhough the array to archive and delete the files.
Any help would be appreciated.
Thanks.
Re: Attach Files to an E-Mail and Delete them
You need to close the file. Streams are IDisposable, so call Dispose() immediately you don't need them more, or use using statement over it.
Re: Attach Files to an E-Mail and Delete them
boudino,
Thanks for your reply.
This is where I am calling the class.
SendEmail.SendMessage(sendTo, " ", emsubject,files);
files is the array of files.
And this is where I am adding the files to the attachments.
foreach (string file in filesToattach)
{
message.Attachments.Add(new Attachment(file));
}
Where and how should I use the Dispose()?
Thanks.
Re: Attach Files to an E-Mail and Delete them
Call it on each item in files.
Re: Attach Files to an E-Mail and Delete them
boudino,
I tried your suggestion, but I still get the error.
Could you provide some sample code?
Thanks.
Re: Attach Files to an E-Mail and Delete them
Found the solution.
The message has to be disposed. In other words, after sending the message, call the dispose.
client.Send(message);
message.dispose();
Hope this helps somebody.