C# でディスク容量が少ないときにGmailからメールを投げるプログラム

C# でディスク容量が少ないときにメールを投げるプログラムを書いた。

環境:

Windows7 64bit Professional

Visual Studio 2013 Express

参考:

ドライブの全体の容量や空き容量を取得する: .NET Tips: C#, VB.NET

C#でGmailからメールを送信する - いてづきブログ

DriveInfoクラスでディスク情報を取得できる。

メール送信は SmtpClient を使えば超簡単。

Gmailを使うので、2段階認証をしている場合はアプリケーション固有のパスワードを発行しておくこと。

あとはこのプログラムをビルドして.exeを作成し、タスクスケジューラに登録しておけば自動的にディスク容量を監視して、少なくなったらメールするシステムのできあがり。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace DiskUsageAlert
{
    class Program
    {
        // 監視対象のドライブ
        static string driveLetter = "D";
        // 空き容量が何%以下になったら警告するか
        static int freeSpacePercentageLimit = 6;

        // Gmailのユーザ名
        static string username = "your_gmail@gmail.com";
        // Gmailのパスワード(二段階認証の場合はアプリケーション固有のパスワードを発行しておく)
        static string password = "yourpassword";
        // GmailのSMTPサーバ
        static string smtpHost = "smtp.gmail.com";
        // GmailのSMTPサーバのポート
        static int smtpPort = 587;
        // 送信先のメールアドレス
        static string sendTo = username;
        // 送信元のメールアドレス
        static string sendFrom = username;

        static void Main(string[] args)
        {
            string hostName = System.Net.Dns.GetHostName();
           
            // ドライブの情報を取得する
            System.IO.DriveInfo drive = new System.IO.DriveInfo(driveLetter);
            //ドライブの準備ができているか調べる
            if (drive.IsReady)
            {
                int freeSpacePercentage = (int)(((double)drive.AvailableFreeSpace / (double)drive.TotalSize) * 100);

                if (freeSpacePercentage <= freeSpacePercentageLimit)
                {
                    string subject = string.Format("[{0}] {1} の空き容量が {2} %になりました。", 
                        hostName, driveLetter, freeSpacePercentage);
                    string body = string.Format("[{0}] {1}: {2} GB / {3} GB (空き容量 {4} %)", 
                        hostName, driveLetter, 
                        drive.AvailableFreeSpace / 1024 / 1024 / 1024, 
                        drive.TotalSize / 1024 / 1024 / 1024, 
                        freeSpacePercentage);
                    sendMail(sendTo, sendFrom, subject, body);
                    //Console.WriteLine(subject);
                    //Console.WriteLine(body);
                };
                //Console.ReadKey();
            }
        }

        private static void sendMail (string sendTo, string sendFrom, string subject, string body)
        {
            // SmtpClient のインスタンスを作成して設定
            var smtp = new System.Net.Mail.SmtpClient();
            smtp.Host = smtpHost;
            smtp.Port = smtpPort;

            // Gmail認証
            smtp.Credentials = new System.Net.NetworkCredential(username, password);

            // SSL
            smtp.EnableSsl = true;

            // 送信メッセージ
            var oMsg = new System.Net.Mail.MailMessage()
            {
                Subject = subject,
                Body = body,
                From = new MailAddress(sendFrom, "DiskUsageAlert")
            };

            // 送信先
            oMsg.To.Add(username);

            //メール送信
            smtp.Send(oMsg);
            smtp.Dispose();
        }
    }
}