1
Answer

File is already using by another process...

Manesh Borase

Manesh Borase

15y
3.4k
1
Hi friends,

I am one problem. Please help me to sort out this.

I am send email with attachments. It is working fine.
I want to do is that, after sending the mail, I want to move the files which i attached to the mail from it's original location to other location in my hard drive. 

But When I am doing this, It gives me the exception "File is already in use by another process"

Code:
SendMessageWithAttachment() this method actually send the mail.
 public static string SendMessageWithAttachment(string sendTo, string sendFrom, string sendSubject, string sendMessage, ArrayList attachments)
        {
            try
            {
                message = new MailMessage(
                   sendFrom,
                   sendTo,
                   sendSubject,
                   sendMessage);
                
                foreach (string attach in attachments)
                {
                    Attachment attached = new Attachment(attach, MediaTypeNames.Application.Octet);
                    message.Attachments.Add(attached);
                }
               
                client = new SmtpClient(Properties.Settings.Default.SMTPAddress);

                NetworkCredential basicAuthentication = new NetworkCredential(Properties.Settings.Default.Username, Properties.Settings.Default.Password);
                client.EnableSsl = true;
                client.Port = Convert.ToInt32(Properties.Settings.Default.Port);
                client.UseDefaultCredentials = false;
                client.Credentials = basicAuthentication;
                // send message
                client.Send(message);                 
                
                return "Message sent to " + sendTo + " at " + DateTime.Now.ToString() + ".";
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();               
            }
        }

Here I am invoking the above method

 ArrayList sendAttachments = new ArrayList(Directory.GetFiles("InProcess Mails"));
 
string status = Emailer.SendMessageWithAttachment(sendTo, sendFrom, sendSubject, sendMsgBody, sendAttachments);
            
 MessageBox.Show(status, "Email Status");
 foreach (string str in sendAttachments)
 {
        File.Move(str, "SentMail\\" + str.Substring(str.LastIndexOf('\\')));    <--- This is the line where exception is through
 }
            

Please solve my this problem.

Thanks & regards,
Manesh
Answers (1)
0
Vasanth
NA 2.3k 309.6k 15y


Just simply resolve by ,


After the email is sent use Dispose() method of MailMessage. So here its release all resources and others are disposed here.


And also you can create MailMessage within using block. As a result all resources will be released (disposed or closed) automatically when execution of the block completes.
 


using(MailMessage mmEmail = new MailMessage(...))
{
}

I specially like the alternative solution for "USING". and its very popular in the programming end.

Please mark as answer , if it helps you.