locked
system.net.mail Attching file gets locked RRS feed

  • Question

  • Hi,

    I'm having a problem when I send an attachment using system.net.mail, the file gets locked so that I cannot delete it until the application has finished.

    I found an article 'Re: Delayed send with smtpclient in .net 2.0' that details a similar issue and have followed all the advice in it but i'm still having locking problems.

    Here is my code

    Dim smtp As New SmtpClient(lSMTPServer_Address, lSMTPServer_Port)
    Dim file As Integer


    lMailMessage = New MailMessage
    lMailMessage.From = New MailAddress("emailaddress")
    lMailMessage.To.Add("emailaddress")
    lMailMessage.Subject = "Test"
    lMailMessage.Body = "Test"
    lMailMessage.Attachments.Add(New Mail.Attachment("C:\Test.csv"))

    smtp.ServicePoint.MaxIdleTime = 1000

    Try
        smtp.Send(lMailMessage)

        Send = True
    Catch a As SmtpException
        ErrorMessage = "Error Sending Email" & vbCrLf & "Error : " & a.Message

        If Not (a.InnerException Is Nothing) Then
            ErrorMessage = ErrorMessage & " " & a.InnerException.Message
        End If

        Send = False
    End Try

    smtp = Nothing

    lMailMessage.Attachments.Clear()
    lMailMessage.Attachments.Dispose()
    lMailMessage.Dispose()

    lMailMessage = Nothing

    My.Computer.FileSystem.DeleteFile("C:\Test.csv")

    The email I sent ok, but when I try to delete the file, I get an error saying it is being used by another process.

    Thanks.

    Andy

    Friday, March 10, 2006 9:30 AM

Answers

  • It seems that we are not releasing the file. you can workaround this
    by disposing the attachment - before you call clear.
    here is the complete sample

    Imports System

    Imports System.Net

    Imports System.Net.Mail

    Module Module1

    Sub Main()

    Dim smtp As New SmtpClient("host")

    Dim send As Boolean

    Dim ErrorMessage As String

    Dim lMailMessage As MailMessage = New MailMessage()

    lMailMessage.From = New MailAddress("a@x.com")

    lMailMessage.To.Add("a@x.com")

    lMailMessage.Subject = "Test"

    lMailMessage.Body = "Test"

    lMailMessage.Attachments.Add(New Mail.Attachment("C:\UpdatePatch.log"))

    smtp.UseDefaultCredentials = True

    Try

    smtp.Send(lMailMessage)

    Send = True

    Catch a As SmtpException

    ErrorMessage = "Error Sending Email" & vbCrLf & "Error : " & a.Message

    If Not (a.InnerException Is Nothing) Then

    ErrorMessage = ErrorMessage & " " & a.InnerException.Message

    End If

    Send = False

    End Try

    smtp = Nothing

    For Each a As Attachment In lMailMessage.Attachments

    a.Dispose()

    Next

    lMailMessage.Attachments.Dispose()

    lMailMessage.Dispose()

    lMailMessage = Nothing

    Try

    My.Computer.FileSystem.DeleteFile("C:\UpdatePatch.log")

    Catch a As Exception

    Console.WriteLine(a.ToString())

    End Try

     

    End Sub

    End Module

    Friday, March 10, 2006 3:03 PM
    Moderator

All replies

  • It seems that we are not releasing the file. you can workaround this
    by disposing the attachment - before you call clear.
    here is the complete sample

    Imports System

    Imports System.Net

    Imports System.Net.Mail

    Module Module1

    Sub Main()

    Dim smtp As New SmtpClient("host")

    Dim send As Boolean

    Dim ErrorMessage As String

    Dim lMailMessage As MailMessage = New MailMessage()

    lMailMessage.From = New MailAddress("a@x.com")

    lMailMessage.To.Add("a@x.com")

    lMailMessage.Subject = "Test"

    lMailMessage.Body = "Test"

    lMailMessage.Attachments.Add(New Mail.Attachment("C:\UpdatePatch.log"))

    smtp.UseDefaultCredentials = True

    Try

    smtp.Send(lMailMessage)

    Send = True

    Catch a As SmtpException

    ErrorMessage = "Error Sending Email" & vbCrLf & "Error : " & a.Message

    If Not (a.InnerException Is Nothing) Then

    ErrorMessage = ErrorMessage & " " & a.InnerException.Message

    End If

    Send = False

    End Try

    smtp = Nothing

    For Each a As Attachment In lMailMessage.Attachments

    a.Dispose()

    Next

    lMailMessage.Attachments.Dispose()

    lMailMessage.Dispose()

    lMailMessage = Nothing

    Try

    My.Computer.FileSystem.DeleteFile("C:\UpdatePatch.log")

    Catch a As Exception

    Console.WriteLine(a.ToString())

    End Try

     

    End Sub

    End Module

    Friday, March 10, 2006 3:03 PM
    Moderator
  • Excellent that fixed the problem, many thanks.

     

    Andy

    Friday, March 10, 2006 4:03 PM
  • Thanks, I had the same problem and this code fixed it for me 2.
    Tuesday, June 3, 2008 2:34 PM
  • Over 5 years and people are still having this problem it seems. This seemed to do the trick for me as well. I wasn't aware that mail held access to files even after sending. Could someone maybe explain why when i was creating an .xlsx file i was able to delete it using system.io.file.delete(filename) yet when switching the file to .csv the attachment wasn't releasing the file? 

     

    Note, in both cases i closed and disposed all readers/writers and streams. 

    Wednesday, July 13, 2011 1:54 PM