SMTP Send is locking up my files – c#

How are you reading the files to create the email message? They should be opened as read-only, with a FileShare set to FileShare.ReadWrite… then they shouldn’t be locked. If you are using a FileStream you should also wrap your logic in the using keyword so that the resource is disposed properly.

Update:

I believe disposing the mail message itself will close resources within it and unlock the files:

using (var mail = new MailMessage())
{
    AddAttachments(mail);
}
// File copy code should work here

Leave a Comment