public void SendMail(string address, EmailTemplate template, Dictionary <string, string> replace, List <Tuple <string, Stream> > attachments) { var bodyDocument = new TextDocument(); bodyDocument.Load(template.Body); bodyDocument.Replace(replace); var body = bodyDocument.ToString(); var subjectDocument = new TextDocument(); subjectDocument.Load(template.Subject); subjectDocument.Replace(replace); var subject = subjectDocument.ToString(); var mesage = new MailMessage { Body = body, Subject = subject, IsBodyHtml = true, Priority = MailPriority.Normal, DeliveryNotificationOptions = DeliveryNotificationOptions.Delay | DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.OnSuccess, }; mesage.To.Add(address); if (attachments != null && attachments.Count > 0) { foreach (var attachment in attachments) { mesage.Attachments.Add(new Attachment(attachment.Item2, attachment.Item1)); } } SendMail(mesage); }
private EmailTemplate GetEmailTemplate() { string exePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); string file = Path.Combine(exePath, "Templates", "Report1_EmailTemplate"); string content = ""; if (File.Exists(file)) { content = File.ReadAllText(file); } else { content = @"<div data-subject='KA Reporting Service - Journal Status'> <p>Dear admin</p> <p>Please see the attachement for status of current journals.</p> <p>Regards,</p> <p>Reporting Service<br /> KA Logistic<br /> </p> </div>"; } var emailTemplate = new EmailTemplate() { Body = content }; try { var xdoc = XDocument.Parse(emailTemplate.Body); emailTemplate.Subject = (string)xdoc.Root.Attribute("data-subject"); } catch { emailTemplate.Subject = "KA Reporting Service - Journal Status"; } return(emailTemplate); }
public void Execute() { Console.WriteLine("Execute"); try { var setting = AppSettings.Current.GetString("Report1:Settings"); NameValueSettings settings = new NameValueSettings(setting); var email = settings.Get("email"); if (string.IsNullOrEmpty(email)) { Console.WriteLine("Email was not configured"); } else { EmailTemplate template = null; using (var db = new DataContext()) { var journals = db.DBModel.Journals .Include(x => x.JournalDrivers) .Where(x => x.Status == JournalStatus.Started).ToArray(); bool isChanged = false; var engine = new FileHelperEngine <JournalRow>(); var rows = new List <JournalRow>(); rows.Add(new JournalRow { Shipmentcode = "Shipment Code", TruckNo = "Truck No", DriverName = "Driver", Start = "Start", Destination = "Destination", TotalDistance = "Total Distance", TotalDuration = "Total Duration", CurrentLocation = "Current Location", EstimatedDistanceToDestination = "Estimated Distance To Destination", Note = "Note", }); foreach (var journal in journals) { var journalRows = ToJournalRows(db, journal, ref isChanged); if (journalRows.Length > 0) { rows.AddRange(journalRows); } } //MemoryStream ms = new MemoryStream(); string fileContent = engine.WriteString(rows); string fileName = $"JournalStatus{DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss")}.csv"; // Email if (template == null) { template = GetEmailTemplate(); } (new SmtpEmailService()).SendMail( email, template, new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase) { }, new List <Tuple <string, Stream> >() { new Tuple <string, Stream>(fileName, new MemoryStream(Encoding.Unicode.GetBytes(fileContent))), }); if (isChanged) { db.SaveChanges(); } //ms.Dispose(); } } } catch (Exception ex) { Console.WriteLine("Error:", ex.GetFullMessage()); } finally { Console.WriteLine("Sleep to next run"); } }