[RESOLVED] Attach Files to an E-Mail and Delete them
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] Attach Files to an E-Mail and Delete them

  1. #1
    Join Date
    Sep 2008
    Posts
    91

    [RESOLVED] Attach Files to an E-Mail and Delete them

    Hi all,

    I am having a hard time deleting some files that I am attaching to an E-mail. After I send them, I want to archive them and delete them. The archive goes fine, but as soon as I try to delete them, I get an error stating "The file is in use by another process". I already tried by clearing the array I use to attach the files, but it does not help. This is what I am doing.

    1. Create an array of file names within the directory and send it to a class, which is responsible for sending the E-mail with the attachments.

    2. After the E-mail is sent, control is sent to the main code and I loop trhough the array to archive and delete the files.


    Any help would be appreciated.

    Thanks.

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Attach Files to an E-Mail and Delete them

    You need to close the file. Streams are IDisposable, so call Dispose() immediately you don't need them more, or use using statement over it.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Sep 2008
    Posts
    91

    Re: Attach Files to an E-Mail and Delete them

    boudino,

    Thanks for your reply.

    This is where I am calling the class.

    SendEmail.SendMessage(sendTo, " ", emsubject,files);

    files is the array of files.

    And this is where I am adding the files to the attachments.

    foreach (string file in filesToattach)
    {
    message.Attachments.Add(new Attachment(file));

    }

    Where and how should I use the Dispose()?

    Thanks.

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Attach Files to an E-Mail and Delete them

    Call it on each item in files.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  5. #5
    Join Date
    Sep 2008
    Posts
    91

    Re: Attach Files to an E-Mail and Delete them

    boudino,

    I tried your suggestion, but I still get the error.

    Could you provide some sample code?

    Thanks.

  6. #6
    Join Date
    Sep 2008
    Posts
    91

    Re: Attach Files to an E-Mail and Delete them

    Found the solution.

    The message has to be disposed. In other words, after sending the message, call the dispose.

    client.Send(message);

    message.dispose();

    Hope this helps somebody.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center