public static IEnumerable <Cpty> ToCpties(string SelectedArea, IEnumerable <FileInfo> pdfFilles, IEnumerable <Cpty> savedCpties) { return(pdfFilles .Select(pdf => new { Name = pdf.Name.Split('_')[0], pdf }) .GroupBy(n => n.Name, (k, lst) => { Cpty found = savedCpties.FirstOrDefault(c => c.Name == k && c.BusinessArea == SelectedArea); return new Cpty() { Name = k, BusinessArea = SelectedArea, EMail = found?.EMail ?? "", Active = found?.Active ?? true, pdfFilles = lst .Select(elem => elem.pdf) }; })); }
internal async Task ForwardItems() { Status = "Forwarding... pls wait!"; Outlook.Folder folder = null; Cpty[] cpties = new Cpty[0]; IEnumerable <FileInfo> pdfFilles = null; if (SelectedArea == AuthFwd) { folder = GetOutlookFolder(); if (folder == null) { return; } if (folder.Items.Count == 0) { Status = "No folder/items"; return; } } else { if (!GetPdfFiles(out pdfFilles)) { return; } } try { if (SelectedArea != AuthFwd) { var savedCpties = await Cpty.Read(cpty_path); cpties = PdfHelper.ToCpties(SelectedArea, pdfFilles, savedCpties).ToArray(); var dialog = new Counterparties(); dialog.DataContext = new PdfHelper() { Cpties = cpties }; dialog.ShowDialog(); var allCpties = cpties.Concat(savedCpties.Where(s => s.BusinessArea != SelectedArea)); await Cpty.Save(allCpties, ConfigurationManager.AppSettings["cptiesPath"], txt => Status = txt); } await Task.Run(() => { if (SelectedArea == AuthFwd) { foreach (var obj in folder.Items) { if (obj is Outlook.MailItem mailItem) { #if DEBUG Debug.WriteLine("forwarding mail: " + mailItem.Subject); var recipientNames = new List <string>(); foreach (var objRecipient in mailItem.Recipients) { if (objRecipient is Outlook.Recipient recipient) { Outlook.PropertyAccessor pa = recipient.PropertyAccessor; string smtpAddress = pa.GetProperty(PR_SMTP_ADDRESS).ToString(); recipientNames.Add($"{recipient.Name} <{smtpAddress}>"); } } Debug.WriteLine($"sent to {String.Join("; ", recipientNames)}"); #endif var newItem = mailItem.Forward(); newItem.To = AddressTo; ComposeMail(newItem); } } } else { foreach (var cpty in cpties.Where(c => c.Active)) { Outlook.MailItem mail = application.CreateItem( Outlook.OlItemType.olMailItem) as Outlook.MailItem; mail.Subject = SelectedArea + " - Netting Statement: " + cpty.Name; mail.To = cpty.EMail; foreach (var pdf in cpty.pdfFilles) { mail.Attachments.Add(pdf.FullName, Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing); } ComposeMail(mail); } } } ); Status = "Forward done."; } catch (Exception exc) { Status = exc.Message; } }