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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Juan_BarreraCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Routers

From novice to tech pro — start learning today.