Click to See Complete Forum and Search --> : Error erasing a file


fallnwrld
February 6th, 2009, 04:45 PM
I have the .Net program sending an email and adding a file as an attachment


Private Sub SendSMTP(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strBody As String, ByVal strCC As String, ByVal strAttachments As String)
Dim insMail As New MailMessage(New MailAddress(strFrom), New MailAddress(strTo))
With insMail
.Subject = strSubject
.Body = strBody
'.CC.Add(New MailAddress(strCC))
If Not strAttachments.Equals(String.Empty) Then
Dim strFile As String
Dim strAttach() As String = strAttachments.Split(";"c)
For Each strFile In strAttach
.Attachments.Add(New Attachment(strFile.Trim()))
Next
strFile = Nothing

End If
End With
Dim smtp As New System.Net.Mail.SmtpClient
smtp.Host = strSMTP
smtp.Port = 25
smtp.Send(insMail)

smtp = Nothing
insMail = Nothing
strAttachments = Nothing

End Sub


When I tried to erase the file at the end it says the the file is still in use. I tried declaring the variables to nothing and still doesn't help. If I dont send the file in the email I can erase it with out a problem. Any suggestions?

dglienna
February 6th, 2009, 04:57 PM
Is it getting sent? Sounds like it's only getting queued, and that would lock it.

Do you have Outlook sending immediately? (I don't and that happens in VB6)

Marraco
February 6th, 2009, 05:01 PM
I have the .Net program sending an email and adding a file as an attachment
...I would check that the mail server has released the file. (don't know how)

jmedved
February 7th, 2009, 12:37 AM
When I tried to erase the file at the end it says the the file is still in use. I tried declaring the variables to nothing and still doesn't help. If I dont send the file in the email I can erase it with out a problem. Any suggestions?
Declaring variables to nothing will not do you much good. You should Dispose of MailMessage once you are done with it (also check Using statement).
If that does not help, download Process Explorer (http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx) to check who is actually holding your file.

HanneSThEGreaT
February 7th, 2009, 02:18 AM
Have you tried using Application.DoEvents before deleting the file ¿
I know some may have issues with DoEvents, but I honestly think it may be worth at least a shot :)