File locked after sending it as attachment

前端 未结 6 1444
-上瘾入骨i
-上瘾入骨i 2020-12-10 13:20

I am sending a file as an attachment:

            // Create  the file attachment for this e-mail message.
            Attachment data = new Attachment(filePa         


        
6条回答
  •  余生分开走
    2020-12-10 13:46

    Here's another way to dispose of the Attachments, once you've finished with them...

    //  Prepare the MailMessage "mail" to get sent out...
    await Task.Run(() => smtp.Send(mail));
    
    //  Then dispose of the Attachments.
    foreach (Attachment attach in mail.Attachments)
    {
        //  Very important, otherwise the files remain "locked", so can't be deleted
        attach.Dispose();
    }
    

提交回复
热议问题