2
Answers

Filewatcher with email alert

Jig Pat

Jig Pat

14y
2.4k
1
Hi everyone,

According to below script, instead of getting changed file alert in the text box, can I setup as email alert?

After all my tries,  I get email when I click on Monitor button but not when the file data actually get changed.....

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.IO;

namespace FileMonitor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void fileWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
txtLog.Text = e.ChangeType ": " e.FullPath "\r\n";
txtLog.Focus();
txtLog.Select(txtLog.TextLength, 0);
txtLog.ScrollToCaret();
}

private void fileWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
txtLog.Text = e.ChangeType ": " e.FullPath "\r\n";
txtLog.Focus();
txtLog.Select(txtLog.TextLength, 0);
txtLog.ScrollToCaret();
}

private void fileWatcher_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
txtLog.Text = e.ChangeType ": " e.FullPath "\r\n";
txtLog.Focus();
txtLog.Select(txtLog.TextLength, 0);
txtLog.ScrollToCaret();
}

private void fileWatcher_Renamed(object sender, System.IO.RenamedEventArgs e)
{
txtLog.Text = e.ChangeType ": " e.OldFullPath " renamed to " e.FullPath;
txtLog.Select(txtLog.TextLength, 0);
txtLog.ScrollToCaret();
}

private void btnMonitor_Click(object sender, System.EventArgs e)

{
btnStop.Enabled = true;
fileWatcher.EnableRaisingEvents = true;
MailMessage msg = new MailMessage("[email protected]", "[email protected]");
msg.Subject = "Email Test";
msg.Body = "Submitted By: xxx";
msg.Body = "Alert";
msg.From = "[email protected]";
msg.To = "[email protected]";
SmtpClient SmtpMail = new SmtpClient("localhost");
SmtpMail.Host = "smtp.gmail.com";
SmtpMail.Credentials = CredentialCache.DefaultNetworkCredentials;
SmtpMail.Port = 576;
SmtpMail.Timeout = 50000;
SmtpMail.EnableSsl = true;
SmtpMail.Credentials = new System.Net.NetworkCredential("username", "password");

try
{
SmtpMail.Send(msg);
}

catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}


btnMonitor.Enabled = false;

}

private void btnStop_Click(object sender, EventArgs e)

{
btnMonitor.Enabled = true;
btnStop.Enabled = false;
fileWatcher.EnableRaisingEvents = false;
}

private void btnFolderTree_Click(object sender, EventArgs e)
{
DialogResult result = ofdOpenFile.ShowDialog();
if (result == DialogResult.OK)
{
txtPath.Text = Path.GetFullPath(ofdOpenFile.FileName);
}

}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

Answers (2)