Sending SMTP mail vb.net holds handle to file

I have an application that monitors a folder for incoming files then emails them out.  The problem is it locks the file and It can't be deleted.
It sends the email fine, but once sent, it retains the handle to that file and won't release it.

I want to immediately delete the file after I send it.  I've tried both Emailserver.SendAsync and Emailserver.send and both lock the file.

Any ideas how to release the lock?




Public Sub sendSMTP(ByVal strMsg As String, ByVal strTo As String, ByVal strSubject As String, ByVal strFile As String)
        Dim EmailServer As New System.Net.Mail.SmtpClient(Me.SMTPGW)
        Dim EmailMessage As New System.Net.Mail.MailMessage(Me.EmailFrom, strTo, strSubject, strMsg)
        Dim attachment As New System.Net.Mail.Attachment(strFile)
        EmailMessage.Attachments.Add(attachment)
        Try
            EmailServer.SendAsync(EmailMessage, Me)
        Catch ex As Exception
            msgbox(ex.message)
        End Try
    End Sub

Open in new window

LVL 27
PberSolutions ArchitectAsked:
Who is Participating?
 
Juan_BarreraConnect With a Mentor Commented:
Hi Piber, can you please give this a try? :

 Public Sub sendSMTP(ByVal strMsg As String, ByVal strTo As String, ByVal strSubject As String, ByVal strFile As String)
        Using EmailMessage As New System.Net.Mail.MailMessage(Me.EmailFrom, strTo, strSubject, strMsg)
            Dim EmailServer As New System.Net.Mail.SmtpClient(Me.SMTPGW)
            Dim attachment As New System.Net.Mail.Attachment(strFile)
            EmailMessage.Attachments.Add(attachment)
            Try
                EmailServer.Send(EmailMessage)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Using
    End Sub

Open in new window

0
 
BTosonCommented:
The only thing that comes to mind now to ensure that the file is completely under your control is by doign somethign like this:
Dim attachment As New System.Net.Mail.Attachment(New MemoryStream(IO.File.ReadAllBytes("PathToFile")), "FileName.txt")

I know it's not ideal performance wise etc but it should work!
0
 
PberSolutions ArchitectAuthor Commented:
Thanks for the input.
I'm away from my development computer today, so I've give those solutions a try next week.
Thanks again.
0
 
PberSolutions ArchitectAuthor Commented:
It is definetly the following line causing the lock:
Dim attachment As New System.Net.Mail.Attachment(strFile)
BToson,
I tried the New.MemoryStream, but it had security issues reading the folder path.  Not sure why.  I just couldn't get it to work.
Juan_Barrera,
Sweet, "Using" worked perfectly with the Send method.
Thanks
 
0
 
PberSolutions ArchitectAuthor Commented:
Thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.