private void SendMail(FileInfo fileInfo, string filename, string fullPath)
{
if (Settings.FullDirectoryPathInSubject)
{
filename = fullPath;
}
mailSent = false;
MailAddress toAddress = new MailAddress(Settings.EmailTo);
MailAddress fromAddress = new MailAddress(Settings.EmailFrom);
MailMessage mail = new MailMessage(fromAddress, toAddress);
mail.Subject = string.Format("{0} {1}", Settings.EmailFilter, filename); ;
mail.Body = filename;
Attachment attachment = new Attachment(fullPath);
mail.Attachments.Add(attachment);
SmtpClient smtpClient = new SmtpClient(Settings.SmtpServer, Settings.SmtpPort);
smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted); smtpClient.SendAsync(mail, fileInfo);
while (!mailSent)
{
Thread.Sleep(500);
}
mail.Dispose();
}
void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
FileInfo fileInfo = (FileInfo)e.UserState;
string filename = Path.GetFileName(fileInfo.FullName);
Console.WriteLine(">>>> File {0} sent", filename);
if (e.Error != null)
{
Console.WriteLine("[{0}] {1}", fileInfo.FullName, e.Error.ToString());
}
mailSent = true;
}