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
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
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.
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
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:
Dim counter As Collections.ObjectModel.ReadOnlyCollection(Of String)
counter = My.Computer.FileSystem.GetFiles("C:\Temp\")
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.
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
Re: Email all files in a directory as attachment
Could you just put 'Exit For' ?
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?
Re: Email all files in a directory as attachment
What about this:
vb Code:
Dim counter As Collections.ObjectModel.ReadOnlyCollection(Of String)
For Each foundDirectory As String In _
My.Computer.FileSystem.GetDirectories("Top level directory path here", _
FileIO.SearchOption.SearchAllSubDirectories)
counter = My.Computer.FileSystem.GetFiles(foundDirectory & "\")
If counter.Count > 0 Then 'At least 1 file
For Each file As String In My.Computer.FileSystem.GetFiles(foundDirectory & "\")
'Add attachment etc...
Me.ListBox1.Items.Add(file)
Next
End If
Next
Could also have (and would probably make more sense) this loop:
vb Code:
If Counter.Count > 0 Then
For i As Integer = 0 To counter.Count - 1
'Add attachment etc...
Me.ListBox1.Items.Add(counter(i).ToString)
Next
End If