Data Cogs Information Technology

posts - 131, comments - 251, trackbacks - 36

General

Search This Site

Powered by Google

Brisbane

Locations of visitors to this page

Information

Archives

Images

Blogs

CV etc.

Links For Me

I recently aquired a GMail account, but having 3 email addresses already, I didn't have a lot of use.  That was until I got the idea that I can use it to back stuff up.

I thought that I might write a Windows service, using the FileSystemWatcher class that could monitor a folder for new files.  If I want to backup a file, all I have to do is drag it to my “gmail“ folder and the service will automatically zip it up and email it to my GMail account.

I used SharpZipLib (an open source bit of C# code) to do the zipping and the SmtpMail .NET class to do the emailing.

It ended up just being a few lines of code...

This is the code for the Created event of the FileSystemWatcher:

private void fsw_Created(object sender, FileSystemEventArgs e)
{
 eventLog1.WriteEntry("File added: " + e.FullPath);
 //zip up new file and send to GMail
 //if zip exentension just send it else zip if first
 string extension = "";
 if (e.FullPath.LastIndexOf(".") > 0)
 {
  extension = e.FullPath.Substring(e.FullPath.LastIndexOf("."));
 }
 

 if (extension.ToLower() != ".zip")
 {
  zipFile(e.FullPath);
 }
 else
 {
  sendToGmail(e.FullPath);
 }
}

And this is the code to send the file to GMail:

private void sendToGmail(string file)
{
 string smtpserver = ConfigurationSettings.AppSettings["SMTPServer"];
 string to =  ConfigurationSettings.AppSettings["To"];
 string from =  ConfigurationSettings.AppSettings["From"];
 SmtpMail.SmtpServer = smtpserver;
 eventLog1.WriteEntry("Emailing " + file);
 MailMessage mail = new MailMessage();
 mail.To = to;
 mail.From = from;
 mail.Subject = "Backup " + DateTime.Now.ToString("yyyyMMdd") ;
 mail.Body = "Here is your backup";
 mail.Attachments.Add(new MailAttachment(file, MailEncoding.Base64));
 SmtpMail.Send(mail);
}

You can download the full FolderWatcher class here

posted on Thursday, October 14, 2004 5:59 PM

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Url
Comment   
Protected by Clearscreen.SharpHIPEnter the code you see: