/// <summary> /// /// </summary> /// <param name="objItems">List of EmailItem</param> /// private static void ProcessItemJob(EmailItem emailItem) { Console.WriteLine("{0} ===> {1}", new FileInfo(emailItem.RootFile).Name, emailItem.PostionInFile); SmtpClient client = cl;// SmtpClients.Take(); SendMail(emailItem, client); SmtpClients.Add(client); emailItem.PresistentReport(); if (int.Parse(ConfigurationManager.AppSettings["Interval"]) != 0) { Thread.Sleep(int.Parse(ConfigurationManager.AppSettings["Interval"])); } }
public static void SendMail(EmailItem email, SmtpClient client) { MailMessage message = new MailMessage(); try { message.To.Add(email.To); message.From = new MailAddress(email.From, email.FromName, Encoding.GetEncoding(email.Encoding)); message.Subject = email.Subject; message.Body = email.MailBody; message.BodyEncoding = Encoding.GetEncoding(email.Encoding); message.SubjectEncoding = Encoding.GetEncoding(email.Encoding); message.IsBodyHtml = email.IsHtmlBody; if (email.AttachmentPath != "" && email.AttachmentPath != null) { Attachment data = new Attachment(email.AttachmentPath); data.NameEncoding = Encoding.GetEncoding(email.Encoding); message.Attachments.Add(data); } if (!string.IsNullOrEmpty(email.BCC)) { message.Bcc.Add(email.BCC); } if (!string.IsNullOrEmpty(email.CC)) { message.CC.Add(email.CC); } client.Send(message); email.ProcceType = "Succeeded"; } catch (Exception ex) { email.ProcceType = "Failed"; email.ProcceMessage = ex.ToString(); logger.Log(string.Format("Failed to send email. Email Position: {0}#{1} Subject:{2}", email.RootFile, email.PostionInFile, email.Subject), ex); } }
public static void ProcessFile(string fullPath) { try { string culture = ConfigurationManager.AppSettings["DefaultCulture"]; string fromIndex = ConfigurationManager.AppSettings["From"]; string fromNameIndex = ConfigurationManager.AppSettings["FromName"]; string isHtmlBodyIndex = ConfigurationManager.AppSettings["IsHtmlBody"]; string toIndex = ConfigurationManager.AppSettings["To"]; string subjectIndex = ConfigurationManager.AppSettings["Subject"]; string bodyIndex = ConfigurationManager.AppSettings["MailBody"]; string attachmentPathIndex = ConfigurationManager.AppSettings["AttachmentPath"]; string bccIndex = ConfigurationManager.AppSettings["BCC"]; string ccIndex = ConfigurationManager.AppSettings["CC"]; logger.Log(string.Format("Start to process file from {0}", fullPath)); CsvConfiguration config = new CsvConfiguration(string.IsNullOrEmpty(culture) ? CultureInfo.CurrentCulture : CultureInfo.GetCultureInfo(culture)); using (CsvReader reader = new CsvReader(new StreamReader(fullPath), config)) { long counter = 0; reader.Read(); reader.ReadHeader(); while (reader.Read()) { counter++; EmailItem item = new EmailItem(); item.RootFile = fullPath; item.PostionInFile = counter; try { item.From = string.IsNullOrEmpty(fromIndex) ? item.From : reader.GetField(fromIndex); item.FromName = string.IsNullOrEmpty(fromNameIndex) ? item.FromName : reader.GetField(fromNameIndex); item.IsHtmlBody = string.IsNullOrEmpty(isHtmlBodyIndex) ? item.IsHtmlBody : bool.Parse(reader.GetField(isHtmlBodyIndex)); item.To = reader.GetField(toIndex); if (subjectIndex.StartsWith("~")) { string subjectRealValue = ConfigurationManager.AppSettings[reader.GetField(subjectIndex.TrimStart('~'))]; item.Subject = subjectRealValue; } else { item.Subject = reader.GetField(subjectIndex); } if (!string.IsNullOrEmpty(bccIndex)) { if (bccIndex.StartsWith("~")) { item.BCC = ConfigurationManager.AppSettings[bccIndex.TrimStart('~')] .Replace('|', ','); } else { item.BCC = reader.GetField(bccIndex) .Replace('|', ','); } } if (!string.IsNullOrEmpty(ccIndex)) { if (ccIndex.StartsWith("~")) { item.CC = ConfigurationManager.AppSettings[ccIndex.TrimStart('~')] .Replace('|', ','); } else { item.BCC = reader.GetField(ccIndex) .Replace('|', ','); } } if (bodyIndex.StartsWith("~~")) { item.MailBody = File.ReadAllText(reader.GetField(bodyIndex.TrimStart('~'))); } else { item.MailBody = reader.GetField(bodyIndex); } foreach (string key in TemplatePlaceHolders.Keys) { if (reader.Context.HeaderRecord.Contains(TemplatePlaceHolders[key])) { item.MailBody = item.MailBody.Replace(key, reader.GetField(TemplatePlaceHolders[key])); } } item.AttachmentPath = string.IsNullOrEmpty(attachmentPathIndex) ? null : reader.GetField(attachmentPathIndex); ProcessItemJob(item); } catch (Exception e) { item.ProcceType = "Failed"; item.ProcceMessage = e.ToString(); logger.Log("Failed to create an email item {0}", e); } } } } catch (Exception e) { logger.Log(string.Format("Fail to process file from {0}", fullPath), e); Console.WriteLine("Fail to process file from {0} , please check if the specific file is a supported file. Error details has been saved in logfile.", fullPath); } }