• Status: Solved
• Priority: Medium
• Security: Public
• Views: 651

# Send email when new file is created in network folder

Hi in work we have a system that creates .tif documents and puts them into a folder if a reject happens for some reason. The folder is on our network eg. N:\Readsoft\Rejects

As it is at the moment people have to manually go in to this folder and check the rejects. Looking for a way to send an email each time a new document is created in the folder. Is This possible through SQL or VB.NET?

I had a stab at doing it through vb.net. I can retrieve the file names and the created date. So I could try and write some code for if the created date happen in the last x amount of hours. But wondering is there anyway I can get it to work when the file is created sort of like the way you can have a trigger on a database
Imports System.Net.Mail

Module SendEmail

Sub Main()
Dim Directory As New IO.DirectoryInfo("c:\test")
Dim allFiles As IO.FileInfo() = Directory.GetFiles("*.*", IO.SearchOption.TopDirectoryOnly)
Dim rejects As New ArrayList(2)

For i = 0 To allFiles.Length - 1
Next
MsgBox("There are " & allFiles.Length & " Emails In The Rejects Folder. An Email Has been sent to The Appropriate supervisor")
Dim SendTo(1) As String
Dim FileAttach(2) As String
Dim strSubject As String
Dim strMessage As String

SendTo(0) = "gdunbar@cook.ie"
SendTo(1) = "gdunbar@cook.ie"

FileAttach(0) = "c:\text.txt"
FileAttach(1) = "c:\Othertext.txt"

strSubject = "Email Subject"

strMessage = "Email Messge Text" 'The body encoding is set to HTML

SendEmail.SendEmailMessage("fromsomeone@email.com", SendTo, strSubject, strMessage, FileAttach)
End Sub

Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo() _
As String, ByVal strSubject _
As String, ByVal strMessage _
As String, ByVal fileList() As String)
'This procedure takes string array parameters for multiple recipients and files
Try
For Each item As String In strTo
'For each to address create a mail message
'MailMsg.BodyEncoding = Encoding.Default
MailMsg.Subject = strSubject.Trim()
MailMsg.Body = strMessage.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True

'attach each file attachment
For Each strfile As String In fileList
If Not strfile = "" Then
Dim MsgAttach As New Attachment(strfile)
End If
Next

'Smtpclient to send the mail message
Dim SmtpMail As New SmtpClient
SmtpMail.Host = "10.35.50.64"
SmtpMail.Send(MailMsg)
Next
'Message Successful
Catch ex As Exception
'Message Error
End Try
End Sub

End Module

0
GlobexCorp
• 2
1 Solution

Senior .Net ConsultantCommented:
in vb.net, you can use the FileSystemWatcher component. see http://www.emoreau.com/Entries/Articles/2005/04/The-FileSystemWatcher-component.aspx
0

Author Commented:
Thanks for the reply I've tried using the FileSystemWatcher class and it does the business my only problem with this however is that I need to check 8 folders. I don't seem to be able to do it for multiple folders is there any way I can do this with the FileSystemWatcher or could you suggest an alternative to do this without having to wrrite 8 applications thanks.
0

Author Commented:
Thanks for the link on the filesystemwatcher I got it sorted now
Dim watchers As New ArrayList()

For Each dir As String In watchers
Dim FileSystemWatcher As New IO.FileSystemWatcher
FileSystemWatcher.Path = dir
FileSystemWatcher.Filter = "*.tif"
FileSystemWatcher.NotifyFilter = (NotifyFilters.LastWrite Or _
NotifyFilters.FileName Or _
NotifyFilters.DirectoryName)