private void AddOrAppendToMessages(List <MailAggregatedMessage> msgs, string toAddress, string subject, string body) { string currentSubject = subject; if (aggregatedSubject.Length > 0 && !aggregateBySubject) { currentSubject = aggregatedSubject; } if ((from msg in msgs where msg.ToAddress.ToUpper() == toAddress.ToUpper() && (!aggregateBySubject || msg.Subject.ToUpper() == subject.ToUpper()) select msg).Count() == 0) { //add new instance MailAggregatedMessage newMsg = new MailAggregatedMessage() { ToAddress = toAddress, Subject = currentSubject, Body = MessageFromTemplate("1", subject, body), MsgRepeatCounter = 1, MsgAggrCounter = 1 }; msgs.Add(newMsg); } else { MailAggregatedMessage lastMsg = (from msg in msgs where msg.ToAddress.ToUpper() == toAddress.ToUpper() && (!aggregateBySubject || msg.Subject.ToUpper() == subject.ToUpper()) orderby msg.MsgRepeatCounter descending select msg).First(); if ((lastMsg.Body.Length + MessageSeparatorTemplate.Length + "Message X\r\nSubject:".Length + subject.Length + body.Length) < maxMsgSize) { lastMsg.MsgAggrCounter++; lastMsg.Body += MessageFromTemplate(lastMsg.MsgAggrCounter.ToString(), subject, body); } else { int msgCounter = lastMsg.MsgRepeatCounter + 1; MailAggregatedMessage newMsg = new MailAggregatedMessage() { ToAddress = toAddress, Subject = currentSubject, Body = MessageFromTemplate("1", subject, body), MsgRepeatCounter = msgCounter, MsgAggrCounter = 1 }; msgs.Add(newMsg); } } }
public List <MailAggregatedMessage> GetMessages() { List <MailAggregatedMessage> msgs = new List <MailAggregatedMessage>(); try { if (inputFilePath.Length > 0 && System.IO.Directory.Exists(inputFilePath)) { foreach (string fileName in System.IO.Directory.GetFiles(inputFilePath, inputFileMask)) { string[] lines = "".Split(); try { lines = System.IO.File.ReadAllLines(fileName); } catch (Exception linesEx) { if (linesEx.Message.Contains("The process cannot access the file")) { lines = "".Split(); } else { throw; } } if (lines.Length == 0) { RaiseAggregatorError(string.Format("The file {0} is empty!", fileName)); } else { string msgToAddress = GetMsgToAddressFromFile(lines); string subject = GetMsgSubjectFromFile(lines); string currentSubject = subject; string body = GetMsgBodyFromFile(lines); if (aggregatedSubject.Length > 0 && !aggregateBySubject) { currentSubject = aggregatedSubject; } if ((from msg in msgs where msg.ToAddress.ToUpper() == msgToAddress.ToUpper() && (!aggregateBySubject || msg.Subject.ToUpper() == subject.ToUpper()) select msg).Count() == 0) { //add new instance MailAggregatedMessage newMsg = new MailAggregatedMessage() { ToAddress = msgToAddress, Subject = currentSubject, Body = MessageFromTemplate("1", subject, body), // "Message 1\r\nSubject:" + subject + "\r\n" + body, MsgRepeatCounter = 1, MsgAggrCounter = 1 }; msgs.Add(newMsg); } else { MailAggregatedMessage lastMsg = (from msg in msgs where msg.ToAddress.ToUpper() == msgToAddress.ToUpper() && (!aggregateBySubject || msg.Subject.ToUpper() == subject.ToUpper()) orderby msg.MsgRepeatCounter descending select msg).First(); if ((lastMsg.Body.Length + MessageSeparatorTemplate.Length + "Message X\r\nSubject:".Length + subject.Length + body.Length) < maxMsgSize) { lastMsg.MsgAggrCounter++; lastMsg.Body += MessageFromTemplate(lastMsg.MsgAggrCounter.ToString(), subject, body); // "\r\nMessage " + lastMsg.MsgAggrCounter.ToString() + "\r\nSubject:" + subject + "\r\n" + body; } else { int msgCounter = lastMsg.MsgRepeatCounter + 1; MailAggregatedMessage newMsg = new MailAggregatedMessage() { ToAddress = msgToAddress, Subject = currentSubject, Body = MessageFromTemplate("1", subject, body), // "Message 1\r\nSubject:" + subject + "\r\n" + body, MsgRepeatCounter = msgCounter, MsgAggrCounter = 1 }; msgs.Add(newMsg); } } } try { if (removeFilesOnDone) { System.IO.File.Delete(fileName); } else if (appendDoneFiles) { if (!System.IO.File.Exists(fileName + ".done")) //Does not exist yet { System.IO.File.Move(fileName, fileName + ".done"); } else { if (appendDoneFileMaxSizeKB > 0) //Max size for append file specified { System.IO.FileInfo fi = new System.IO.FileInfo(fileName + ".done"); if ((fi.Length / 1024) > appendDoneFileMaxSizeKB) //newest append file over size limit { RenameExistingAppendFiles(fileName, 0); //increase counter and rename all previous append files System.IO.File.Move(fileName, fileName + ".done"); } else //newest append file still under limit { System.IO.File.AppendAllText(fileName + ".done", "\r\n" + System.IO.File.ReadAllText(fileName)); } } else //No max size for append file specified { System.IO.File.AppendAllText(fileName + ".done", "\r\n" + System.IO.File.ReadAllText(fileName)); } System.IO.File.Delete(fileName); } } else //try to rename { if (System.IO.File.Exists(fileName + ".done")) //try to remove it { System.IO.File.Delete(fileName + ".done"); } System.IO.File.Move(fileName, fileName + ".done"); } } catch (Exception renex) { RaiseAggregatorError(renex.ToString()); } } } else { throw new Exception(string.Format("Input directory not found or invalid! ({0})", inputFilePath)); } } catch (Exception ex) { RaiseAggregatorError(ex.ToString()); } return(msgs); }