2017-08-07 3 views
0

ディレクトリ内のlastwritetimeを参照する電子メール警告を送信しようとしています.15分を超えるものは電子メール警告を送信します。ここに私のコードと私がやろうとしていることがあります。ディレクトリ内の15分を超える警告メールファイル

 MailMessage CyclopsCentral = new MailMessage(); 
     SmtpClient client = new SmtpClient(server); 
     CyclopsCentral.From = new System.Net.Mail.MailAddress("[email protected]"); 
     CyclopsCentral.To.Add("[email protected]"); 
     CyclopsCentral.Subject = "Cyclops Central Not Updating"; 
     CyclopsCentral.Body = "Cyclops Central is not updating, please check the management center."; 
     CyclopsCentral.Priority = MailPriority.High; 


     try 
     { 
      string[] files = Directory.GetFiles(@"\\Users\!NCDSPOTVIEW\Desktop\ScreenScrapes\Chicago\ScreenScrape\ScreenScrape.png\"); 


      foreach (string file in files) 
      { 
       FileInfo fi = new FileInfo("ScreenScrape.png"); 
       if (fi.LastWriteTime < DateTime.Now.AddHours(-1)) 
        fi.Send(CyclopsCentral); 
      } 
      //client.LogFileName = "d:\\smtp.txt"; 
      Console.WriteLine("Start to send email ..."); 
      //client.Send(CyclopsCentral); 
      Console.WriteLine("email was sent successfully"); 
     } 

     catch (Exception ep) 
     { 
      Console.WriteLine("failed to send email with the following error:"); 
      Console.WriteLine(ep.Message); 
     } 
    } 
} 

}

+1

コードの問題点は何ですか?それは例外をスローしますか?頻繁にメールを送信しますか?それはうまくいったが、メールは受信されないと言うか? (ちなみに、 "client.Send"の行はコメントアウトされています) –

+0

あなたはDirectory.GetFiles()の出力を使用していません(結果を丸めていますが、コード内のファイルを使用していません)。 –

+0

また、プライバシー上の理由から、あなたのコードからメールアドレスを削除したいかもしれません。 –

答えて

0

あなたはDirectory.GetFiles()から返されたファイルのリストを使用していません。代わりにこれを試してみてください:

foreach (string file in files) 
{ 
    FileInfo fi = new FileInfo(file); 
    if (fi.LastWriteTime < DateTime.Now.AddMinutes(-15)) 
    { 
     client.Send(CyclopsCentral); 
    } 
} 

また、あなただけに関係なく、それはあなたがclient.Sendbreakステートメントを追加することも検出されたファイルの数の1通の電子メールをしたい場合。

関連する問題