Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
VB
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
'AttchFile = "Unit1_" & ReportDate.ToShortDateString & ".PDF," & "Unit2_" & ReportDate.ToShortDateString & ".PDF," & "Common_" & ReportDate.ToShortDateString & ".PDF"
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

' AttchFile = "Report_" & ReportDate.ToShortDateString & ".xls"
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
' MySMTP.Timeout = 600000
Dim Send As SendingEmail = AddressOf SendMail '(EMail, MySMTP, 600000, Date.Now)
Dim AsyncResult As IAsyncResult = Send.BeginInvoke(EMail, MySMTP, 600000, Date.Now, AddressOf CallBackHandler, Send)
SendingTime = Now
'*****************************Put it back after debug********************
'BtnSendMail.Enabled = False
'***********************************************************************
End Sub

A long execution method
VB
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
VB
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
VB
Private Sub FrmNL_WKVReport_SendMailSuccessfully() Handles Me.SendMailSuccessfully
'Dim Files As List(Of FileInfo) = AttachedFiles.ToList
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
VB
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.
Posted
Updated 10-Dec-15 20:23pm
v2
Comments
CHill60 13-Dec-15 15:29pm    
That empty Catch block in the Callback handler is not a good idea!
LoboMan 21-Dec-15 16:57pm    
I have to agree with CHill60. Putting a little logging code in there or even throwing (BOO HISS) the error might yield more information that could be helpful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900