The process cannot access the file because it is being used by another process.

I am creating a file on the local drive with the contents using the code below.

File.WriteAllLines(path, contents); 

I attach this file to the mail and send acrross to the command. After sending the mail, I need to delete the file in order to delete the file that I use below, but I get a runtime error

 File.Delete(path); 

Error message : the process cannot access the file because it is being used by another process

by default, the WriteAllLines () method closes the file, but still it is opened by another process. I can only delete the file by running the code after a while, but this is not a script. I need to delete it after sending mail.

Update

 System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); mailMessage.To.Add(new System.Net.Mail.MailAddress(recipient, ToName)); mailMessage.From = new System.Net.Mail.MailAddress(From, FromName); mailMessage.Subject = Subject; // "Outlook calendar as attachment"; // modified by Srikanth J on 28/06/2012 mailMessage.Body = "This is a test message"; System.Net.WebClient webclient = new System.Net.WebClient(); webclient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; for (int i = 0; i < item.Attachments.Count; i++) { string url = item.Attachments.UrlPrefix + item.Attachments[i]; SPFile file = item.ParentList.ParentWeb.GetFile(url); mailMessage.Attachments.Add(new System.Net.Mail.Attachment(file.OpenBinaryStream(), file.Name)); } System.Net.Mail.Attachment mailAttachment = new System.Net.Mail.Attachment(path); mailMessage.Attachments.Add(mailAttachment); smtp.Send(mailMessage); 

Any help is provided, thanks.

+6
source share
5 answers

MailMessage implements IDisposable , so you should use the using keyword to free up any resources when you are done with it. If you do not, yes, it is very good that the file remains in use until the garbage collector has noticed that you are no longer using the message.

 using (var mailMessage = new MailMessage()) { // set mailMessage properties // ... smtp.Send(mailMessage); } 

You can also call Dispose for attachments directly, but deleting the message will already ensure that all subobjects, including attachments, are correctly configured.

+14
source

Just add a simple Using statement to

  System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); 

to change in this way

  using(System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage()) { ....... smtp.Send(mailMessage); } 

when your code exits the using statement, the mailMessage object will be deleted, as well as each object that implements the IDisposable interface. This means that each individual application in the attachment collection will be deleted, and your files will no longer be locked.

+2
source

If you want to do this, I would suggest writing the file to a file in Stream, not a file.

Thus, you can completely eliminate the problem of blocking files.

An example is shown here showing how to set up an attachment based on a stream (the code for recording the stream itself is not shown here).

 private static void SendReport(Report report) { MailMessage msg = new MailMessage { From = new MailAddress(Configuration.EmailFrom), Subject = Configuration.EmailSubject, Body = Configuration.EmailBody }; msg.To.Add(Configuration.EmailTo); if (!string.IsNullOrWhiteSpace(Configuration.EmailCC)) msg.CC.Add(Configuration.EmailCC); if (!string.IsNullOrWhiteSpace(Configuration.EmailBcc)) msg.Bcc.Add(Configuration.EmailBcc); Program.AttachReport(report, msg); SmtpClient smtp = new SmtpClient(); smtp.Send(msg); } private static void AttachReport(Report report, MailMessage message) { Stream stream = new MemoryStream(); report.Save(stream, Configuration.SurveyName); message.Attachments.Add(new Attachment(stream, Configuration.AttachmentName, Configuration.AttachmentMediaType)); } 
+1
source

try it

 File.WriteAllLines(path, contents); using(System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage()) { // send mail containing the file here } File.Delete(path); 

Hope this helps you.

+1
source

EDIT

 try { //send mail code } finally { mailAttachment=null; mailMessage=null; } //delete file code 

I suggest that after sending mail at the end, you will either go to the file descriptor or close the file

 try { //attach file //send mail } finally { //set finally handle to null // or close the file } //after this delete the file 
0
source

Source: https://habr.com/ru/post/926923/


All Articles