public Batch(Email email, MailMessage message, MailboxProfile profile) { Documents = new List <Document>(); this.emailID = email.EmailID; this.batchNumber = email.BatchNumber; this.profile = profile; var folder = String.Format(String.IsNullOrWhiteSpace(profile.OutputFolderFormat) ? "{0:00000000}" : profile.OutputFolderFormat, emailID); if (profile.BatchStyle == BatchStyle.ImagesOnly || !profile.BatchClassSubfolder) { OutputPath = Path.Combine(profile.OutputPath, folder); } else { OutputPath = Path.Combine(profile.OutputPath, String.Format(@"{0}\{1}", profile.BatchClassName, folder)); } FileSystemHelper.CleanDirectory(OutputPath, true); SaveMessage(email); InitialiseFields(message); ExtractDocuments(message, false); FileSystemHelper.DeleteDirectory(Path.Combine(OutputPath, "temp"), true); }
public ErrorBatch(Email email, MailMessage message, MailboxProfile profile) { Documents = new List <Document>(); this.emailID = email.EmailID; this.batchNumber = email.BatchNumber; this.profile = profile; this.errorreason = email.Status == "Rejected" ?string.Join(",", email.Errors.Descendants("Error") .Select(e => (string)e.Attribute("fileName") + " - " + (string)e.Attribute("reason")) .ToList()) : "Unsupported File Type"; var folder = String.Format(String.IsNullOrWhiteSpace(profile.OutputFolderFormat) ? "{0:00000000}" : profile.OutputFolderFormat, emailID); if (profile.BatchStyle == BatchStyle.ImagesOnly || !profile.BatchClassSubfolder) { OutputPath = Path.Combine(profile.ErrorOutputPath, folder); } else { OutputPath = Path.Combine(profile.ErrorOutputPath, String.Format(@"{0}\{1}", profile.BatchClassName, folder)); } FileSystemHelper.CleanDirectory(OutputPath, true); SaveMessage(email); InitialiseFields(message); //ExtractDocuments(message, false); FileSystemHelper.DeleteDirectory(Path.Combine(OutputPath, "temp"), true); }
private void Initialise(Email email, MailboxProfile profile) { // Assign parameters to class members this.email = email; this.profile = profile; // Load the Mail Message var options = new MailMessageLoadOptions(); options.FileCompatibilityMode = FileCompatibilityMode.SkipValidityChecking; options.MessageFormat = email.MessageFilePath.ToLower().EndsWith("msg") ? MessageFormat.Msg : MessageFormat.Eml; this.message = MailMessage.Load(email.MessageFilePath, options); }
public void BeginAsync(Email email, MailboxProfile profile) { Initialise(email, profile); ManualResetEvent waiter = new ManualResetEvent(false); //var folder = String.Format(String.IsNullOrWhiteSpace(profile.OutputFolderFormat) ? "{0:00000000}" : profile.OutputFolderFormat, emailID); //OutputPath = Path.Combine(profile.OutputPath, folder); System.Threading.Tasks.Task.Run(() => { Worker(waiter); }); waiter.WaitOne(); }
private void AddProfile(MailboxProfile profile) { var group = listViewProfiles.Groups[profile.Group ?? "Default"]; if (group == null) { group = listViewProfiles.Groups.Add(profile.Group ?? "Default", profile.Group ?? "Default"); } var item = listViewProfiles.Items.Add(profile.MailboxGUID.ToString(), profile.Description, null); item.Tag = profile; item.Group = group; }
public Boolean ContainsMethod(Session session, MailboxProfile profile, String methodName) { if (session == null || String.IsNullOrWhiteSpace(methodName)) { return(false); } if (profile.ScriptEntryPoints == null || !profile.ScriptEntryPoints.Contains(methodName)) { return(false); } return(true); }
public Object Execute(Session session, MailboxProfile profile, String methodName) { if (session == null || String.IsNullOrWhiteSpace(methodName)) { return(null); } if (profile.ScriptEntryPoints == null || !profile.ScriptEntryPoints.Contains(methodName)) { return(null); } return(session.Execute(String.Format("{0}();", methodName))); }
private void ShowEmails(MailboxProfile profile) { Cursor = Cursors.WaitCursor; listViewEmails.Items.Clear(); var emails = GetEmails(profile); using (EmailImportDataContext dc = new EmailImportDataContext()) { foreach (var email in emails) { // If LiteViewer Mode then check if emails Profile is Lite Viewer Enabled if (LiteViewerMode && !ProfileLiteViewerEnabled((Guid)email.MailboxGUID)) { continue; } ListViewItem lvi = new ListViewItem(new string[] { email.From, email.Subject, email.DateSent.HasValue ? email.DateSent.Value.ToString() : String.Empty, email.DateReceived.HasValue ? email.DateReceived.Value.ToString() : String.Empty, Convert.ToString(email.ProcessedCount.GetValueOrDefault()), email.Status, email.StartTime.HasValue ? email.StartTime.Value.ToString() : String.Empty, email.EndTime.HasValue ? email.EndTime.Value.ToString() : String.Empty, email.Errors != null ? "Y" : String.Empty }); lvi.Tag = email; listViewEmails.Items.Add(lvi); } }; // If ListView contains Items then Select First 1 else display Message if (listViewEmails.Items.Count > 0) { listViewEmails.Items[0].Selected = true; } else { ListViewItem lvi = new ListViewItem(new string[] { null, NOEMailsMessage }); listViewEmails.Items.Add(lvi); } Cursor = Cursors.Default; }
public void BeginAsync(Email email, MailboxProfile profile) { if (email == null) { throw new ArgumentNullException("email", "Value cannot be null."); } if (profile == null) { throw new ArgumentNullException("profile", "Value cannot be null."); } Initialise(email, profile); CreateScriptSession(); ExecuteScript("MailMessage_Loaded"); if (context != null && context.IgnoreMessage) { UpdateStatus(EmailStatus.Ignored); ConfigLogger.Instance.LogInfo(email.EmailID, "Message ignored via script."); } else { UpdateStatus(EmailStatus.InProgress); ManualResetEvent waiter = new ManualResetEvent(false); System.Threading.Tasks.Task.Run(() => { Worker(waiter); }); waiter.WaitOne(); } }
private List <Email> GetEmails(MailboxProfile profile) { IEnumerable <Email> emails; int numberOfEmails = Int32.Parse(toolStripTextBoxMaximumEmails.Text); using (EmailImportDataContext dc = new EmailImportDataContext()) { if (profile != null) { emails = dc.Emails.Where(e => e.MailboxGUID == profile.MailboxGUID); } else { emails = dc.Emails; } switch (filter.Type) { case FilterType.DateSent: emails = emails.Where(e => e.DateSent >= filter.Date1 && e.DateSent < filter.Date2.AddDays(1)); break; case FilterType.DateReceived: emails = emails.Where(e => e.DateReceived >= filter.Date1 && e.DateReceived < filter.Date2.AddDays(1)); break; case FilterType.Status: emails = emails.Where(e => e.Status == filter.StringValue); break; case FilterType.BatchNumber: emails = emails.Where(e => e.BatchNumber.ToLower() == filter.StringValue.ToLower()); break; case FilterType.From: emails = emails.Where(e => e.From.ToLower().Contains(filter.StringValue.ToLower())); break; case FilterType.Subject: emails = emails.Where(e => e.Subject.ToLower().Contains(filter.StringValue.ToLower())); break; case FilterType.MessageID: emails = emails.Where(e => e.MessageID.ToLower().Contains(filter.StringValue.ToLower())); break; case FilterType.EmailID: emails = emails.Where(e => e.EmailID == filter.LongValue); break; } emails = emails.Take(numberOfEmails); return(emails.ToList()); } }
public Imap(MailboxProfile profile) : base(profile.ImapHost, profile.ImapPort, profile.ImapUserName, profile.ImapPassword, profile.ImapFolder) { }
private int DownloadEmail(Imap imap, MailboxProfile profile, String storagePath) { int count = 0; // Build the MailQuery var query = new MailQuery(String.IsNullOrEmpty(profile.ImapQuery) ? "('Deleted' = 'False')" : String.Format("({0}&('Deleted' = 'False'))", profile.ImapQuery)); // Get all messages matching to the query var infos = imap.ListMessages(query); // If there are any messages to process, then process them if (infos.Any()) { ConfigLogger.Instance.LogInfo("ImapCollector", String.Format("Downloading {0} message{1} from {2}.", infos.Count, infos.Count == 1 ? "" : "s", profile.Description)); // Download each message foreach (var info in infos) { if (!timer.Enabled) { break; } // Just check to ensure its valid if (info.Deleted || String.IsNullOrWhiteSpace(info.UniqueId)) { continue; } // Track the start time for debug purposes var start = DateTime.Now; MailMessage message = null; try { // Download the message message = imap.FetchMessage(info.UniqueId); // Calculate the time taken to fetch the message var fetchTime = DateTime.Now.Subtract(start); // Process the message (So long as the fetch succeeded) if (message != null) { // Setup the data context using (var ctx = new EmailImportDataContext()) { long emailID = 0; // Truncate the subject to avoid data commit errors message.Subject = Truncate(message.Subject, 500); // Check for duplicate if (IsDuplicate(ctx, profile.MailboxGUID, message, ref emailID)) { // Log the duplicate error ConfigLogger.Instance.LogWarning("ImapCollector", String.Format("Message already downloaded, moved to duplicate folder (existing EmailID = {0}).", emailID)); // Move the message to the duplicate sub folder imap.MoveMessage(info.UniqueId, "Duplicate", true, false); } else { // Create an instance of the email database object var email = new Email(); // Assign properties email.MailboxGUID = profile.MailboxGUID; email.DateSent = message.DateSent(); email.DateReceived = message.DateReceived(); email.From = message.From.GetAddressOrDisplayName(); email.MessageID = message.MessageId; if (CreditCardHelper.ExistsCCNumber(message.Subject)) { email.Subject = CreditCardHelper.MaskCCNumbers(message.Subject, '#'); } else { email.Subject = message.Subject; } email.Timestamp = DateTime.Now; // Create the dated storage path var path = Path.Combine(storagePath, email.Timestamp.Value.ToString("yyyyMMdd")); FileSystemHelper.CreateDirectory(path); // Insert the new record ctx.Emails.InsertOnSubmit(email); // Submit the email (we need to get the email ID) using (TransactionScope scope = new TransactionScope()) { // Initial submit of changes ctx.SubmitChanges(); // Build the mail message file name email.MessageFilePath = Path.Combine(path, String.Format("{0:00000000}.eml", email.EmailID)); // Save to disk (delete anything that already exists) message.Save(email.MessageFilePath, MessageFormat.Eml); // Get the batch number - THIS SHOULD NEVER HAPPEN IN A MULTI THREAD SCENARIO WITHOUT A LOCK var batchNumber = ctx.BatchNumbers.SingleOrDefault(b => b.Group == profile.Group); // If there is no batchNumber defined yet, create and insert one if (batchNumber == null) { batchNumber = new BatchNumber(); batchNumber.Group = profile.Group; ctx.BatchNumbers.InsertOnSubmit(batchNumber); } // Init to 0 if null if (!batchNumber.Value.HasValue) { batchNumber.Value = 0; } // Set the new batch number to this email email.BatchNumber = String.Format(String.IsNullOrWhiteSpace(profile.BatchNumberFormat) ? "{0:00000000}" : profile.BatchNumberFormat, ++batchNumber.Value); // Final submit of updates ctx.SubmitChanges(); // Finally, commit to the database scope.Complete(); } // Move the email to the archive (if this fails, but the download is complete this // will just result in a duplicate next time round if the deleted flag is not set) imap.MoveMessage(info.UniqueId, "Archive", true, false); // Log message level download stats ConfigLogger.Instance.LogDebug("ImapCollector", String.Format("Message downloaded (EmailID = {0}, Fetch Time = {1}s, Total Time = {2}s).", email.EmailID, (int)fetchTime.TotalSeconds, (int)DateTime.Now.Subtract(start).TotalSeconds)); // Increment the download count count++; } } } } catch (OutOfMemoryException) { throw; } catch (Exception e) { ConfigLogger.Instance.LogError("ImapCollector", e); } finally { if (message != null) { message.Dispose(); } } } } return(count); }
private Boolean UploadMessage(String fileName, MailboxProfile profile, String storagePath) { var options = new MailMessageLoadOptions(); options.FileCompatibilityMode = FileCompatibilityMode.SkipValidityChecking; options.MessageFormat = fileName.ToLower().EndsWith("msg") ? MessageFormat.Msg : MessageFormat.Eml; using (MailMessage message = MailMessage.Load(fileName, options)) using (EmailImportDataContext ctx = new EmailImportDataContext()) { // Truncate the subject to avoid data commit errors message.Subject = Truncate(message.Subject, 500); if (!IsDuplicate(ctx, profile.MailboxGUID, message)) { // Create an instance of the email database object var email = new Email(); // Assign properties email.MailboxGUID = profile.MailboxGUID; email.DateSent = message.DateSent(); email.DateReceived = message.DateReceived(); email.From = message.From.GetAddressOrDisplayName(); email.MessageID = message.MessageId; if (ExistsCCNumber(message.Subject)) { email.Subject = MaskCCNumbers(message.Subject, '#'); } else { email.Subject = message.Subject; } email.Timestamp = DateTime.Now; // Create the dated storage path var path = Path.Combine(storagePath, email.Timestamp.Value.ToString("yyyyMMdd")); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } // Insert the new record ctx.Emails.InsertOnSubmit(email); // Submit the email (we need to get the email ID) using (TransactionScope scope = new TransactionScope()) { // Initial submit of changes ctx.SubmitChanges(); // Build the mail message file name email.MessageFilePath = Path.Combine(path, String.Format("{0:00000000}.eml", email.EmailID)); // Copy the eml file if its already in this format, if msg then save as eml if (fileName.EndsWith("eml", StringComparison.OrdinalIgnoreCase)) { File.Copy(fileName, email.MessageFilePath, true); } else { // Save in eml format message.Save(email.MessageFilePath, MessageFormat.Eml); } // Get the batch number - THIS SHOULD NEVER HAPPEN IN A MULTI THREAD SCENARIO WITHOUT A LOCK var batchNumber = ctx.BatchNumbers.SingleOrDefault(b => b.Group == profile.Group); // If there is no batchNumber defined yet, create and insert one if (batchNumber == null) { batchNumber = new BatchNumber(); batchNumber.Group = profile.Group; ctx.BatchNumbers.InsertOnSubmit(batchNumber); } // Init to 0 if null if (!batchNumber.Value.HasValue) { batchNumber.Value = 0; } // Set the new batch number to this email email.BatchNumber = String.Format(String.IsNullOrWhiteSpace(profile.BatchNumberFormat) ? "{0:00000000}" : profile.BatchNumberFormat, ++batchNumber.Value); // Final submit of updates ctx.SubmitChanges(); // Finally, commit to the database scope.Complete(); } return(true); } else { return(false); } } }