Hi all
I develop an app that gather data from equipment then compose a report as attachement and send an Email to list of recipients. After successfully sent I want to delete those attachement but got an exception .
The sending method is running in a separate thread , the send finishing is alert by a callback method and raise an event. After event raising I dispose Email object and thus its attachment (by logic) then waiting for some time for garbage collection do it proper job . In my case about 10 minutes then starting to delete those physical attach files. And always get exception no matter how much I increase a waiting time (from 1-10 minutes)bellow are some code fragment.
User Sending Action
Private Sub BtnSendMail_Click(sender As Object, e As EventArgs) Handles BtnSendMail.Click
Application.DoEvents()
Dim Subject As String = "Operation Report of " & ReportDate.ToLongDateString
Dim AttchFile As String = ""
Dim Files() As FileInfo
If RadPDF.Checked Then
Dim Dir As New DirectoryInfo(PDFRoot)
Files = Dir.GetFiles()
AttachedFiles = Files
For n As Integer = 0 To Files.Count - 1
If n < Files.Count - 1 Then
AttchFile += Files.ElementAt(n).Name & " , "
Else
AttchFile += Files.ElementAt(n).Name
End If
PDFAttach(n) = Files.ElementAt(n).FullName
Next
EMail = New MailMessage("VXXXXXXX@gmail.com", ReceipientAddress.ToString, Subject, ComposeMailBody(ReportDate, AttchFile))
EMail.Attachments.Insert(0, New Attachment(PDFAttach(0)))
EMail.Attachments.Insert(1, New Attachment(PDFAttach(1)))
EMail.Attachments.Insert(2, New Attachment(PDFAttach(2)))
EMail.BodyEncoding = Encoding.Unicode
End If
If RadSheet.Checked Then
Dim Dir As New DirectoryInfo(ExcelRoot)
Files = Dir.GetFiles()
AttachedFiles = Files
AttchFile += Files.ElementAt(0).Name
ExcelAttach = Files.ElementAt(0).FullName
EMail = New MailMessage("VXXXXX@gmail.com", ReceipientAddress.ToString, Subject, ComposeMailBody(ReportDate, AttchFile))
EMail.Attachments.Insert(0, New Attachment(ExcelAttach))
EMail.BodyEncoding = Encoding.Unicode
End If
MySMTP.Host = "smtp.gmail.com"
MySMTP.Port = 587
MySMTP.Credentials = New NetworkCredential("VXXXXX@gmail.com", "XXXXXXXX")
MySMTP.EnableSsl = True
Dim Send As SendingEmail = AddressOf SendMail
Dim AsyncResult As IAsyncResult = Send.BeginInvoke(EMail, MySMTP, 600000, Date.Now, AddressOf CallBackHandler, Send)
SendingTime = Now
End Sub
A long execution method
Public Function SendMail(ByRef MyMail As MailMessage, ByRef MySMTP As SmtpClient, ByVal Timeout As Integer, StartTime As Date) As Date
Dim Ret As Date
MySMTP.Timeout = Timeout
MySMTP.Send(MyMail)
Ret = Now
Return Ret
End Function
A Calback method
Public Sub CallBackHandler(Result As IAsyncResult)
Dim SendingMethod As SendingEmail = DirectCast(Result.AsyncState, SendingEmail)
Try
Completion = SendingMethod.EndInvoke(EMail, MySMTP, Result)
EMail.Attachments.Clear()
RaiseEvent SendMailSuccessfully()
Catch ex As Exception
End Try
End Sub
Event handling method
Private Sub FrmNL_WKVReport_SendMailSuccessfully() Handles Me.SendMailSuccessfully
EMail.Dispose()
FinishSend = Now
Dim T As TimeSpan = Now - SendingTime
TimeSendMail.Text = "Time to Send Mail: " & T.Minutes.ToString() & " Minutes :" & T.Seconds & " Seconds:" & T.Milliseconds & " Milliseconds"
End Sub
Error occurs here
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer2.Enabled = True
Dim T As TimeSpan = Now - FinishSend
If T.TotalMinutes > 1 Then
If AttachedFiles IsNot Nothing Then
For Each f As FileInfo In AttachedFiles
f.Delete()
Next
TimeSendMail.Text = "Email Attached file was deleted"
FinishSend = Date.MaxValue
End If
AttachedFiles = Nothing
End If
End Sub
If any one has an idea how to solve this situation , please advise.