Do one of these two things:
1. Pass a reference of the current email message to the EmailServer_SendCompleted event so that we can do something there.
OR,Code:Private Sub SendEmail() Try Dim strFileSize As String = "" Dim di As New IO.DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\nietverzonden\") Dim aryFi As IO.FileInfo() = di.GetFiles("*.pdf") Dim fi As IO.FileInfo Dim TestEmail As New System.Net.Mail.MailMessage(TextBox2.Text, TextBox3.Text, "Pdf Mailer" + Date.Today, "") 'Attachin pdf to email For Each fi In aryFi Dim i As String i = System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\nietverzonden\" + fi.Name TestEmail.Attachments.Add(New System.Net.Mail.Attachment(i)) Next EmailServer = New System.Net.Mail.SmtpClient(TextBox1.Text) EmailServer.SendAsync(TestEmail, TestEmail) Catch ex As Exception MessageBox.Show(ex.ToString, "Error Sending Email", MessageBoxButtons.OK, MessageBoxIcon.Hand) End Try End Sub Private Sub EmailServer_SendCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles EmailServer.SendCompleted Try If e.Error Is Nothing Then If e.Cancelled Then MessageBox.Show("Test Email Cancelled", "Email Cancelled", MessageBoxButtons.OK, MessageBoxIcon.Warning) Else CType(e.UserState, System.Net.Mail.MailMessage).Attachments(0).ContentStream.Close() 'create folder and move file Dim DirConstr As String DirConstr = System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\verzonden\" & Date.Now.Year If (Not System.IO.Directory.Exists(DirConstr)) Then System.IO.Directory.CreateDirectory(DirConstr) End If 'move pdf Dim path As String = IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "pdf\nietverzonden") Dim filesToMove As String() = IO.Directory.GetFiles(path, "*.pdf", IO.SearchOption.AllDirectories) 'or IO.SearchOption.TopDirectoryOnly For Each file As String In filesToMove Dim filename As String = IO.Path.GetFileName(file) Dim newFile As String = IO.Path.Combine(DirConstr, filename) IO.File.Move(file, newFile) Next End If End If Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub
2. Send email synchronously rather than asynchronous. And after email is sent, close the attachment.
What I showed in the above examples is just releasing one attachment. But since you might have multiple attachments, you should loop through all the attachments and close the stream.Code:Private Sub SendEmail() Try Dim strFileSize As String = "" Dim di As New IO.DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\nietverzonden\") Dim aryFi As IO.FileInfo() = di.GetFiles("*.pdf") Dim fi As IO.FileInfo Dim TestEmail As New System.Net.Mail.MailMessage(TextBox2.Text, TextBox3.Text, "Pdf Mailer" + Date.Today, "") 'Attachin pdf to email For Each fi In aryFi Dim i As String i = System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\nietverzonden\" + fi.Name TestEmail.Attachments.Add(New System.Net.Mail.Attachment(i)) Next EmailServer = New System.Net.Mail.SmtpClient(TextBox1.Text) EmailServer.Send(TestEmail) TestEmail.Attachments(0).ContentStream.Close() Catch ex As Exception MessageBox.Show(ex.ToString, "Error Sending Email", MessageBoxButtons.OK, MessageBoxIcon.Hand) End Try End Sub Private Sub EmailServer_SendCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles EmailServer.SendCompleted Try If e.Error Is Nothing Then If e.Cancelled Then MessageBox.Show("Test Email Cancelled", "Email Cancelled", MessageBoxButtons.OK, MessageBoxIcon.Warning) Else 'create folder and move file Dim DirConstr As String DirConstr = System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\verzonden\" & Date.Now.Year If (Not System.IO.Directory.Exists(DirConstr)) Then System.IO.Directory.CreateDirectory(DirConstr) End If 'move pdf Dim path As String = IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "pdf\nietverzonden") Dim filesToMove As String() = IO.Directory.GetFiles(path, "*.pdf", IO.SearchOption.AllDirectories) 'or IO.SearchOption.TopDirectoryOnly For Each file As String In filesToMove Dim filename As String = IO.Path.GetFileName(file) Dim newFile As String = IO.Path.Combine(DirConstr, filename) IO.File.Move(file, newFile) Next End If End If Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub




icon on the left of the post.
Reply With Quote