Results 1 to 9 of 9

Thread: Email all files in a directory as attachment

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Email all files in a directory as attachment

    How can I email all this files in this directory as attachments?
    Code:
            Dim di As New IO.DirectoryInfo("\\Server\Company\D1\")
            Dim diar1 As IO.FileInfo() = di.GetFiles()
            Dim dra As IO.FileInfo
    
            Dim message As New MailMessage( _
            "[email protected]", _
            "[email protected]", _
            "Test Order", _
            "Test Order")
            For Each dra In diar1
                MailMessage.
            Next
    
            Dim emailClient As New SmtpClient("mail.fe.com")
            emailClient.Send(message)
        End Sub

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Email all files in a directory as attachment

    I would think this would do it fine.

    Code:
            Dim di As New IO.DirectoryInfo("\\Server\Company\D1\")
            Dim diar1 As IO.FileInfo() = di.GetFiles()
            Dim dra As IO.FileInfo
    
            Dim message As New MailMessage( _
            "[email protected]", _
            "[email protected]", _
            "Test Order", _
            "Test Order")
            For Each dra In diar1
                message.Attachments.Add(New Attachment(dra.FullName))
            Next

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Email all files in a directory as attachment

    That works great.

    I am going to try and add some code to clear the files from the directory after an email has been successfully sent but I will try to figure it out before I post the question to this thread. Thanks again.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Email all files in a directory as attachment

    I figured out how to the clear the files from the directory but I still have an issue I can't get resolved. I want to prevent the email being generated if there in no file in the directory. Typically I would just check to see if the file exists but I can't seem to figure out how to do that with For Each Loop.

    Just to clarify there are 10 directories that may or may not have files in them. If only 5 of the directories have files in them only 5 emails will go out. The empty directories will be ignored until the next time the code is run and the directories are checked again.

    Essentially I am just trying to prevent blank or attachmentless emails from being sent.

    Code:
            Dim di As New IO.DirectoryInfo(strFPath)
            Dim diar1 As IO.FileInfo() = di.GetFiles()
            Dim dra As IO.FileInfo
    
            Dim message As New MailMessage
            message.From = New MailAddress(StrMailFrom)
            message.To.Add(strMailTo1)
            message.To.Add(strMailTo2)
            message.To.Add(strMailTo3)
            message.To.Add(strMailTo4)
            message.To.Add(strMailTo5)
            message.To.Add(strmailTo6)
            message.Subject = StrSubject
            message.Body = StrBody
    
            For Each dra In diar1
                message.Attachments.Add(New Attachment(dra.FullName))
            Next
    
            Dim emailClient As New SmtpClient(strMailServer)
            emailClient.Credentials = (SMTP_Cred)
            emailClient.DeliveryMethod = SmtpDeliveryMethod.Network
            emailClient.EnableSsl = True
            Try
                emailClient.Send(message)
            Catch ex As Exception
                MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace)
            End Try
    Last edited by FastEddie; Apr 18th, 2007 at 08:09 AM.

  5. #5
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Email all files in a directory as attachment

    You can get the number of files in a folder by doing something like this:
    vb Code:
    1. Dim counter As Collections.ObjectModel.ReadOnlyCollection(Of String)
    2.         counter = My.Computer.FileSystem.GetFiles("C:\Temp\")
    3.         MessageBox.Show("Found: " & counter.Count.ToString & " files")

    If Counter.Count = 0 then no files exist. Just set up your loops etc... to check the value first. If the directory file count doesn't equal 0 then loop through each file and attach. If number of files in a folder does equal 0 then move on to next folder or whatever.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Email all files in a directory as attachment

    stimbo the loop part is what I am having trouble with. I think my problem is that I need to check each folder before the loop starts so that if there is not a file in the first folder it will skip tha email and go back to the beginning of the loop to check the next folder and so on until it has made it through all 10 directories.

    To sum it up I just need to cancel the current itteration of the loop if there is no mail in a given folder.

    Something like this
    Code:
            For Each dra In diar1
                If dra.Exists Then
                    message.Attachments.Add(New Attachment(dra.FullName))
                Else
                    ' Jump out of current loop
                    MessageBox.Show("Jumped out of email loop")
                End If
            Next

  7. #7
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Email all files in a directory as attachment

    Could you just put 'Exit For' ?
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Email all files in a directory as attachment

    Won't that make me jump out of the loop? Also I don't think I can use the If dra.Exists Then because it seems to allways evaluate to true. How do I test for files using the dra?

  9. #9
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Email all files in a directory as attachment

    What about this:
    vb Code:
    1. Dim counter As Collections.ObjectModel.ReadOnlyCollection(Of String)
    2.  
    3.         For Each foundDirectory As String In _
    4.             My.Computer.FileSystem.GetDirectories("Top level directory path here", _
    5.              FileIO.SearchOption.SearchAllSubDirectories)
    6.  
    7.             counter = My.Computer.FileSystem.GetFiles(foundDirectory & "\")
    8.  
    9.             If counter.Count > 0 Then  'At least 1 file
    10.                 For Each file As String In My.Computer.FileSystem.GetFiles(foundDirectory & "\")
    11.                     'Add attachment etc...
    12.                     Me.ListBox1.Items.Add(file)
    13.                 Next
    14.             End If
    15.  
    16.         Next

    Could also have (and would probably make more sense) this loop:
    vb Code:
    1. If Counter.Count > 0 Then
    2.       For i As Integer = 0 To counter.Count - 1
    3.                  'Add attachment etc...
    4.                  Me.ListBox1.Items.Add(counter(i).ToString)
    5.      Next
    6. End If
    Last edited by stimbo; Apr 18th, 2007 at 07:57 PM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

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