c# - delete attachment file -


I am using System.Net.Mail to send mail in asp.net. As an attachment mail .. I tried to use the file. Delete method .. But I'm getting this error. The process file can not use the path \ fun.jpg 'because it is being used by another process, thank you

Dispose the mailmessage when you have completed it, unless you have done so, you have a lock on the lock placed on the file.

  var filePath = "C: \\ path \\ to \\ file.txt"; Var smtpClient = new SmtpClient ("mailhost"); (Var Message = New MailMessage ()) using {message.To.Add ("to@domain.com"); message. New = MailAddress ("from@domain.com"); Message.Subject = "test"; Message.SubjectEncoding = Encoding.UTF8; message. Body = "test" + date time.Now; message. Attachment.add (new attachment (filepat)); } If (file.exists (file path) file. Delete (file palette); Console.WriteLine (File.Exists (filepath));  

Output: incorrect

I think if you have locked the file after the message is settled, then you have another lock on file , But without code, we can not help you


Comments