|
-
Dec 29th, 2011, 11:23 PM
#1
Thread Starter
Lively Member
[RESOLVED] system.IO.IOException
Hi all
In the following Sub I'm sending an Attachment when the email has been sent I would like to delete the attachment but I get an system.IO.IOException Error. the file I'm trying to delete is being used by another process.
How can I detect if the process has finished.
Thanks
Happy New Year to everyone
Code:
Private Sub SendMail()
Try
Dim cfiAttach As Net.Mail.Attachment
Dim Attacmentfile As String = AttDir & fileName & ".pdf"
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New Net.NetworkCredential(diaSettings.txtMail_User.Text, diaSettings.txtMail_Pass.Text)
SmtpServer.Port = smtpPrt
SmtpServer.Host = SmtpHst
mail = New MailMessage()
mail.From = New MailAddress(Mail_user)
mail.To.Add(Mail_TO)
' mail.Bcc.Add(BBC_MailTO)
mail.Subject = fileName
mail.Body = ""
cfiAttach = New Attachment(Attacmentfile)
If File.Exists(Attacmentfile) Then
cfiAttach = New Attachment(Attacmentfile)
mail.Attachments.Add(cfiAttach)
Else
MsgBox("Attachment Not Found")
End If
SmtpServer.Send(mail)
MsgBox("Sent")
System.IO.File.Delete(AttDir & fileName & ".pdf")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Last edited by viper5646; Dec 29th, 2011 at 11:26 PM.
-
Dec 29th, 2011, 11:47 PM
#2
Re: system.IO.IOException
I wonder whether the Send method actually just passes the request off to some Windows process and then returns, so your file is still in use by that process. I don't actually know but I would have thought not. One point to consider is the fact that the Attachment class implements IDisposable and you are not disposing it. That would be the first thing I'd look at, i.e. Dispose your Attachment and see if that releases the file.
-
Dec 29th, 2011, 11:59 PM
#3
Thread Starter
Lively Member
Re: system.IO.IOException
Thanks jmcilhinney
I tried mail.Attachments.IDisposable() and I get the following error Error 'IDisposable' is not a member of 'System.Net.Mail.AttachmentCollection'.
I also tried mail.Attachments.Disposable I still get system.IO.IOException error.
-
Dec 30th, 2011, 12:16 AM
#4
Re: system.IO.IOException
Jmcilhinney means like this:
Code:
Private Sub SendMail()
Try
Dim cfiAttach As Net.Mail.Attachment
Dim Attacmentfile As String = AttDir & fileName & ".pdf"
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New Net.NetworkCredential(diaSettings.txtMail_User.Text, diaSettings.txtMail_Pass.Text)
SmtpServer.Port = smtpPrt
SmtpServer.Host = SmtpHst
mail = New MailMessage()
mail.From = New MailAddress(Mail_user)
mail.To.Add(Mail_TO)
' mail.Bcc.Add(BBC_MailTO)
mail.Subject = fileName
mail.Body = ""
'cfiAttach = New Attachment(Attacmentfile) 'Commented as not necessary
If File.Exists(Attacmentfile) Then
cfiAttach = New Attachment(Attacmentfile)
mail.Attachments.Add(cfiAttach)
Else
MsgBox("Attachment Not Found")
End If
if cfiAttach isnot nothing then cfiAttach.Dispose
SmtpServer.Send(mail)
MsgBox("Sent")
System.IO.File.Delete(AttDir & fileName & ".pdf")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Kris
-
Dec 30th, 2011, 12:40 AM
#5
Re: system.IO.IOException
Not quite i00. You can't Dispose the Attachment before calling Send. I do mean call the Dispose method but it must be done AFTER sending the mail. Also, there's no point using an If statement because you know for a fact that the Attachment is not Nothing at that point.
That said, the preferred way of disposing an short-lived object is with a Using block. The object is created by the Using statement and implicitly disposed at the End Using statement:
vb.net Code:
Using mailAttachment As New Attachment(filePath) 'Use mailAttachment here. End Using
-
Dec 30th, 2011, 09:00 AM
#6
Thread Starter
Lively Member
Re: system.IO.IOException
Thanks jmcilhinney and i00 for your replies.
There was two calls for the New Attachment(Attacmentfile).
By deleting the first one and using mail.Attachments.Dispose()
did the trick .
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|