Results 1 to 33 of 33

Thread: -=Solved=- Create Directory Problem

Threaded View

  1. #28
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Create Directory Problem

    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.

    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
    OR,
    2. Send email synchronously rather than asynchronous. And after email is sent, close the attachment.
    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
    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.
    Last edited by Pradeep1210; Nov 27th, 2009 at 03:09 PM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width