0

I create an XML file that I need to email to users when they register. After creating the XML file I pass it as an attached filename (with ServeMapPath) and the email programs send the email message with file. Yea. However, I then want to delete that file.

It appears the SmtpClient client = new SmtpClient(settings.SMTPServer); is locking the file after sending email. Even if I wait a long time, it remains locked. Using IISExpress from Visual Studio. If I exist Visual Studio and reopen debugging, I can then delete, or overwrite the file one time.

Is this a characteristic of developing under Visual Studio or ??? any work arounds for testing? Will IIS keep the file locked on a production server?

IrvineCAGuy
  • 211
  • 2
  • 12

1 Answers1

1

Do I earn points for finding the solution to my own problem?

The email send routine creates a connection to SMTPClient, and I created a new Message as well. I did a client.Dispose(), but not for the message. When I did some clean up, like message.Dispose(), the file unlocked and my application could then delete it when done.

SmtpClient client = new SmtpClient(settings.SMTPServer);
MailMessage message = new MailMessage();
....
 //send message with attachment
....
client.Dispose();
message.Dispose();

I hope this helps others.

IrvineCAGuy
  • 211
  • 2
  • 12