Jan 28 2008
Preventing File Locking in E-mail Attachment
I’m trying to attach a file to an e-mail that I’m sending using the SmtpClient class (from System.Net.Mail) using C# for ASP.NET. Everything works correctly, but the problem is that the file that was uploaded gets locked in the folder by Windows and I can’t delete it afterwards.
This is the code I used to attach a file to an e-mail:
sHtmlConfirmation = "D:\\ftpsite\\from_web\\" + sFolder + "\\" + sFolder + ".htm";
Attachment data = new Attachment (sHtmlConfirmation);
Email.Attachments.Add (data);
I needed a solution to prevent Windows from locking a file that it uses, so I turned to the MSDN Forums for help. James Curran posted a working solution. He suggested employing the using statement to dispose the MailMessage object after the mail has been sent.
The code which fixes the problem is:
using (MailMessage Email = new MailMessage ())
{
// setup message details here
}
After the mail was sent, I was able to move the folder where the attached file was used.
Feb 19, 2009 @ 09:16:46
Thank you for your post
I have not idea how to do what you need but made me think about how to do it now.
Thanks!
Jose
Dec 21, 2009 @ 09:14:10
I wasn’t aware of the many ripples and depth to this story until I surfed here through Google! Great job.
.-= اخبار مصر´s last blog ..دار الأوبرا المصرية..الفارق بين فرقة أمريكية وبرودواي!! =-.