?
Solved

Sending SMTP mail vb.net holds handle to file

Posted on 2008-11-06
5
Medium Priority
?
786 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:Pber
  • 3
5 Comments
 
LVL 20

Accepted Solution

by:
Juan_Barrera earned 1000 total points
ID: 22899751
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
 
LVL 4

Expert Comment

by:BToson
ID: 22899754
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
 
LVL 26

Author Comment

by:Pber
ID: 22907121
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
 
LVL 26

Author Comment

by:Pber
ID: 22913028
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
 
LVL 26

Author Closing Comment

by:Pber
ID: 31514122
Thanks again.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the hope of saving someone else's sanity... About a year ago we bought a Cisco 1921 router with two ADSL/VDSL EHWIC cards to load balance local network traffic over the two broadband lines we have, but we couldn't get the routing to work consi…
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question