CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2010
    Posts
    2

    File is already used by another process...

    Hi
    Last edited by manesh.borase; March 31st, 2010 at 08:13 AM.

  2. #2
    Join Date
    Jan 2010
    Posts
    2

    Talking Re: File is already used by another process...

    Hi friends,


    Sorry use this post rather than above one.

    I have 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

  3. #3
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: File is already used by another process...

    1. Welcome
    2. Edit your posts, don't double-post
    3. Please use code-tags. (["code"] and [/"code"] without the quotes)


    Outlook has a habit of keeping a handle on any file it uses until it (Outlook) is closed and reopened. I know that it does this with .msg files which I used in an application not too long ago. My solution was to copy the .msg file to another location before opening it. I don't know if this is the same with the SmtpClient, but I suggest you copy the attachment files before attaching them to an e-mail and see if the error is still occurring. If it is, the problem lies elsewhere
    It's not a bug, it's a feature!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured