System.net.mail Attachment file locks


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: System.net.mail Attachment file locks

  1. #1
    Join Date
    May 2011
    Posts
    2

    System.net.mail Attachment file locks

    Hi,

    I'm struggling with a bit of code to delete files after sending them as attachments.

    I've been looking around and seems I need to dispose the attachments to release the locks, but I can't seem to get it working. I'm setup to send only 10 files at a time, if there are more than 10 files, only the last batch will not delete. The file(s) remain locked until the app is closed.

    Would be much appreciated if someone could point out where I'm going wrong

    Code:
    Sub Sendmail()
            Dim rkey As RegistryKey = Registry.LocalMachine
            Dim rkeyAccounts As RegistryKey = Nothing
            Dim rkeySettings As RegistryKey = Nothing
            Dim rkeySMTP As RegistryKey = Nothing
            Dim aryFi As IO.FileInfo()
            Dim fi As IO.FileInfo
            Try
                rkeyAccounts = rkey.OpenSubKey("Software\Company\PODMailer\Accounts")
    
                Dim a_arr As String() = rkeyAccounts.GetSubKeyNames '
                For Each a As [String] In a_arr
                    rkeySettings = rkey.OpenSubKey("Software\Company\PODMailer\Accounts\" & a)
                    System.Net.Mail.MailMessage()
    
                    Dim mail As New MailMessage
                    Dim Filepath As String = rkeySettings.GetValue("Folder")
                    Dim strFileSize As String = ""
                    Dim di As New IO.DirectoryInfo(Filepath)
                    aryFi = di.GetFiles(rkeySettings.GetValue("FileType"))
    
                    If UBound(aryFi) > -1 Then 'Check to see if any files exist and Email any that do
                        mail.From = New MailAddress("address@domain.com", "Email From Me")
                        mail.To.Add(rkeySettings.GetValue("Email"))
                        mail.Subject = "New POD documents"
                        mail.Body = "Please find attached the following POD documents:<br>"
                        mail.IsBodyHtml = True
                        rkeySMTP = rkey.OpenSubKey("Software\Company\PODMailer")
                        Dim smtp As New SmtpClient(rkeySMTP.GetValue("SMTP"))
                        Dim Filecount As Integer
                        Dim Arysize As Integer = UBound(aryFi) + 1
                        If Arysize > 10 Then Filecount = 10 Else Filecount = Arysize
                        For Each fi In aryFi 'Add files to email
    
                            mail.Body = mail.Body & fi.Name & " (" & Filecount & "/" & Arysize & ")<br>"
                            mail.Attachments.Add(New Attachment(fi.FullName))
                            Filecount -= 1
                            If Filecount = 0 Then
                                smtp.Send(mail)
                                Arysize -= 10
                                If Arysize > 10 Then Filecount = 10 Else Filecount = Arysize
                                mail.Body = "Please find attached the following POD documents:<br>"
                                mail.Attachments.Clear()
                            End If
                        Next
                        mail.Attachments.Dispose()
                        mail.Dispose()
    
                        For Each fi In aryFi 'Delete sent files
                            File.SetAttributes(fi.FullName, FileAttributes.Normal) 'Clear read only attribute
                            fi.Delete()
                        Next
                    End If
                Next
            Catch ex As Exception
                WriteToEventLog(ex.Message & vbCrLf & ex.StackTrace)
    
            Finally
                fi = Nothing
                aryFi = Nothing
    
                rkey.Close()
                rkeyAccounts.Close()
                rkeySettings.Close()
                rkeySMTP.Dispose()
                rkey.Dispose()
                rkeyAccounts.Dispose()
                rkeySettings.Dispose()
            End Try
        End Sub

  2. #2
    Join Date
    May 2011
    Posts
    2
    I've worked out it's something to do with mail.Attachments.Clear() that's causing the file lock, does anyone know why this is or how to get around it?

  3. #3
    Join Date
    Jun 2011
    Posts
    1
    The trouble is the attachment object is holding a lock on the file. Dispose must be called on each attachment object.

    This line locks the file
    >> mail.Attachments.Add(New Attachment(fi.FullName))


    >> Filecount -= 1
    >> If Filecount = 0 Then
    >> smtp.Send(mail)
    >> Arysize -= 10
    >> If Arysize > 10 Then Filecount = 10 Else Filecount = Arysize
    >> mail.Body = "Please find attached the following POD documents:<br>"
    This line releases all the attachments without calling Dispose on them. Do not clear the list.
    >> mail.Attachments.Clear()
    >> End If
    >> Next
    This line just releases the collection, not all the attachments in it. Regardless, there are no attachments left, because the list has been cleared in the code above.
    >> mail.Attachments.Dispose()
    >> mail.Dispose()

    Prior to mail.Attachments.Dispose() you need to iterate through each attachment and call Dispose() on it.

    Even so, your code will have the same problem if an error is thrown. It would be better to use a Try.. Catch.. Finally block and call Dispose() on all the attachments, as well as the mail itself (as long as it has been created first) within the Finally block.

    Hope this helps!

Similar Threads

  1. Replies: 2
    Last Post: 11-28-2008, 07:11 AM
  2. Replies: 1
    Last Post: 02-14-2006, 08:04 AM
  3. Replies: 0
    Last Post: 02-12-2006, 07:27 PM
  4. upload an image file and save on webserver.
    By sandhyaharsh in forum Java
    Replies: 0
    Last Post: 11-15-2005, 07:23 AM
  5. Replies: 2
    Last Post: 03-07-2001, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center