Note that there are some explanatory texts on larger screens.

plurals
  1. POC# using filewatcher to monitor folder to send an email when pdf is received with attachment of pdf
    primarykey
    data
    text
    <p>I have the following code to watch a folder for incoming files. Once the folder receives the files, the program sends an email along with an attachment of the file, in this case, a pdf. However, sometimes we receive more than one file and it sends multiple emails with the same pdf, but with a different file name. Do I have to release the pdf files? I thought I was doing that with the pdfFile.Dispose() and mail.Dispose().</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Net.Mail; namespace Email { class Program { static string files; static void Main(string[] args) { fileWatcher(); } private static void fileWatcher() { try { //Create a filesystemwatcher to monitor the path for documents. string path = @"\\server\folder\"; FileSystemWatcher watcher = new FileSystemWatcher(path); //Watch for changes in the Last Access, Last Write times, renaming of files and directories. watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.CreationTime; watcher.Filter = "FILE*"; //Register a handler that gets called when a file is created. watcher.Created += new FileSystemEventHandler(watcher_Created); //Register a handler that gets called if the FileSystemWatcher need to report an error. watcher.Error += new ErrorEventHandler(watcher_Error); //Begin watching the path for budget documents/ watcher.EnableRaisingEvents = true; Console.WriteLine("Monitoring incoming files for Budget documents."); Console.WriteLine("Please do not close."); Console.WriteLine("Press Enter to quit the program."); Console.ReadLine(); } catch (Exception e) { Console.WriteLine("{0} Exception caught", e); } } //This method is called when a file is created. static void watcher_Created(object sender, FileSystemEventArgs e) { try { //Show that a file has been created WatcherChangeTypes changeTypes = e.ChangeType; Console.WriteLine("File {0} {1}", e.FullPath, changeTypes.ToString()); String fileName = e.Name.ToString(); sendMail(fileName); // throw new NotImplementedException(); } catch (Exception exc) { Console.WriteLine("{0} Exception caught", exc); } } static void watcher_Error(object sender, ErrorEventArgs e) { Console.WriteLine("The file watcher has detected an error."); throw new NotImplementedException(); } private static void sendMail(string fileName) { try { MailMessage mail = new MailMessage(); mail.From = new MailAddress("From@mail.com"); mail.To.Add("Me@mail.com"); string filesDirectory = @"\server\folder\"; string searchForFile = "FILE*"; string[] searchFiles = Directory.GetFiles(filesDirectory, searchForFile); foreach (string File in searchFiles) files = File; Attachment pdfFile = new Attachment(files); mail.Subject = "PDF Files " + fileName; mail.Body = "Attached is the pdf file " + fileName + "."; mail.Attachments.Add(pdfFile); SmtpClient client = new SmtpClient("SMTP.MAIL.COM"); client.Send(mail); //To release files and enable accessing them after they are sent. pdfFile.Dispose(); mail.Dispose(); } catch (Exception e) { Console.WriteLine("{0} Exception caught", e); } } } } </code></pre> <p>Any help would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload