-
April 22nd, 2010, 04:33 PM
#1
[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.
-
April 23rd, 2010, 01:52 AM
#2
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.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
April 23rd, 2010, 10:49 AM
#3
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.
-
April 26th, 2010, 01:51 AM
#4
Re: Attach Files to an E-Mail and Delete them
Call it on each item in files.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
April 26th, 2010, 09:54 AM
#5
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.
-
May 4th, 2010, 03:29 PM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|