CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2003
    Posts
    95

    Error erasing a file

    I have the .Net program sending an email and adding a file as an attachment

    Code:
      Private Sub SendSMTP(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strBody As String, ByVal strCC As String, ByVal strAttachments As String)
            Dim insMail As New MailMessage(New MailAddress(strFrom), New MailAddress(strTo))
            With insMail
                .Subject = strSubject
                .Body = strBody
                '.CC.Add(New MailAddress(strCC))
                If Not strAttachments.Equals(String.Empty) Then
                    Dim strFile As String
                    Dim strAttach() As String = strAttachments.Split(";"c)
                    For Each strFile In strAttach
                        .Attachments.Add(New Attachment(strFile.Trim()))
                    Next
                    strFile = Nothing
    
                End If
            End With
            Dim smtp As New System.Net.Mail.SmtpClient
            smtp.Host = strSMTP
            smtp.Port = 25
            smtp.Send(insMail)
    
            smtp = Nothing
            insMail = Nothing
            strAttachments = Nothing
    
        End Sub
    When I tried to erase the file at the end it says the the file is still in use. I tried declaring the variables to nothing and still doesn't help. If I dont send the file in the email I can erase it with out a problem. Any suggestions?
    Using VB .Net 2008 Express Edition

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Error erasing a file

    Is it getting sent? Sounds like it's only getting queued, and that would lock it.

    Do you have Outlook sending immediately? (I don't and that happens in VB6)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Error erasing a file

    Quote Originally Posted by fallnwrld View Post
    I have the .Net program sending an email and adding a file as an attachment
    ...
    I would check that the mail server has released the file. (don't know how)
    [Vb.NET 2008 (ex Express)]

  4. #4
    Join Date
    May 2004
    Location
    Osijek
    Posts
    61

    Re: Error erasing a file

    Quote Originally Posted by fallnwrld View Post
    When I tried to erase the file at the end it says the the file is still in use. I tried declaring the variables to nothing and still doesn't help. If I dont send the file in the email I can erase it with out a problem. Any suggestions?
    Declaring variables to nothing will not do you much good. You should Dispose of MailMessage once you are done with it (also check Using statement).
    If that does not help, download Process Explorer to check who is actually holding your file.

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Error erasing a file

    Have you tried using Application.DoEvents before deleting the file ¿
    I know some may have issues with DoEvents, but I honestly think it may be worth at least a shot

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