Send email when new file is created in network folder

Posted on 2009-02-16
Last Modified: 2013-11-25
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 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




        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) = ""

        SendTo(1) = ""

        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("", 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


            For Each item As String In strTo

                'For each to address create a mail message

                Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(item))

                '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


                'Smtpclient to send the mail message

                Dim SmtpMail As New SmtpClient

                SmtpMail.Host = ""



            'Message Successful

        Catch ex As Exception

            'Message Error

        End Try

    End Sub

End Module

Open in new window

Question by:GlobexCorp
    LVL 69

    Accepted Solution

    in, you can use the FileSystemWatcher component. see
    LVL 2

    Author Comment

    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.
    LVL 2

    Author Comment

    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 _
                AddHandler FileSystemWatcher.Created, AddressOf FSWHandler1
                AddHandler FileSystemWatcher.Changed, AddressOf FSWHandler1
                AddHandler FileSystemWatcher.Renamed, AddressOf FSWHandlerRename
                FileSystemWatcher.EnableRaisingEvents = True

    Open in new window


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now