private void Run() { try { Status = MessageProcessorStatus.Started; var fullPropertySet = new PropertySet(PropertySet.FirstClassProperties) { EmailMessageSchema.IsRead, EmailMessageSchema.IsReadReceiptRequested, EmailMessageSchema.IsDeliveryReceiptRequested, ItemSchema.DateTimeSent, ItemSchema.DateTimeReceived, ItemSchema.DateTimeCreated, ItemSchema.ItemClass, ItemSchema.MimeContent, ItemSchema.Categories, ItemSchema.Importance, ItemSchema.InReplyTo, ItemSchema.IsFromMe, ItemSchema.IsReminderSet, ItemSchema.IsResend, ItemSchema.IsDraft, ItemSchema.ReminderDueBy, ItemSchema.Sensitivity, ItemSchema.Subject, ItemSchema.Id, ExchangeHelper.MsgPropertyContentType, ExchangeHelper.PidTagFollowupIcon, }; if (service.RequestedServerVersion != ExchangeVersion.Exchange2007_SP1) { fullPropertySet.Add(ItemSchema.ConversationId); fullPropertySet.Add(ItemSchema.IsAssociated); } if (service.RequestedServerVersion == ExchangeVersion.Exchange2013) { fullPropertySet.Add(ItemSchema.ArchiveTag); fullPropertySet.Add(ItemSchema.Flag); fullPropertySet.Add(ItemSchema.IconIndex); } SearchFilter.SearchFilterCollection filter = new SearchFilter.SearchFilterCollection(); filter.LogicalOperator = LogicalOperator.And; if (_startDate != null) { Logger.Debug("Getting mails from " + _startDate); filter.Add(new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, _startDate)); } if (_endDate != null) { Logger.Debug("Getting mails up until " + _endDate); filter.Add(new SearchFilter.IsLessThanOrEqualTo(ItemSchema.DateTimeReceived, _endDate)); } foreach (var exchangeFolder in folders) { ItemView view = new ItemView(_pageSize, 0, OffsetBasePoint.Beginning); view.PropertySet = PropertySet.IdOnly; List <EmailMessage> emails = new List <EmailMessage>(); Boolean more = true; while (more) { try { more = FindExchangeMessages(exchangeFolder, filter, view, emails, fullPropertySet); if (emails.Count > 0) { try { foreach (var emailMessage in emails) { try { String subject; if (!emailMessage.TryGetProperty(ItemSchema.Subject, out subject) || subject == null) { Logger.Warn("Item " + emailMessage.Id.UniqueId + " has no subject assigned, unable to determine subject."); } Logger.Debug("Exporting " + emailMessage.Id.UniqueId + " from " + exchangeFolder.FolderPath + " : " + subject); var flags = new Collection <MessageFlags>(); Boolean flag; if (emailMessage.TryGetProperty(EmailMessageSchema.IsRead, out flag) && !flag) { flags.Add(MessageFlags.Unread); } if (emailMessage.TryGetProperty(ItemSchema.IsDraft, out flag) && flag) { flags.Add(MessageFlags.Draft); } if ( emailMessage.TryGetProperty(EmailMessageSchema.IsReadReceiptRequested, out flag) && flag) { flags.Add(MessageFlags.ReadReceiptRequested); } if ( emailMessage.TryGetProperty( EmailMessageSchema.IsDeliveryReceiptRequested, out flag) && flag) { flags.Add(MessageFlags.DeliveryReceiptRequested); } if (emailMessage.TryGetProperty(ItemSchema.IsReminderSet, out flag) && flag) { flags.Add(MessageFlags.ReminderSet); } if (emailMessage.TryGetProperty(ItemSchema.IsAssociated, out flag) && flag) { flags.Add(MessageFlags.Associated); } if (emailMessage.TryGetProperty(ItemSchema.IsFromMe, out flag) && flag) { flags.Add(MessageFlags.FromMe); } if (emailMessage.TryGetProperty(ItemSchema.IsResend, out flag) && flag) { flags.Add(MessageFlags.Resend); } var message = new RawMessageDescriptor { SourceId = emailMessage.Id.UniqueId, Subject = subject, Flags = flags, RawMessage = "", SourceFolder = exchangeFolder.FolderPath, DestinationFolder = exchangeFolder.MappedDestination, IsPublicFolder = exchangeFolder.IsPublicFolder, }; Object result; if (emailMessage.TryGetProperty(ItemSchema.MimeContent, out result) && result != null) { message.RawMessage = Encoding.UTF8.GetString(emailMessage.MimeContent.Content); } if (emailMessage.TryGetProperty(ItemSchema.ItemClass, out result) && result != null) { message.ItemClass = emailMessage.ItemClass; } if (emailMessage.TryGetProperty(ItemSchema.IconIndex, out result) && result != null) { message.IconIndex = (int)emailMessage.IconIndex; } if (emailMessage.TryGetProperty(ItemSchema.Importance, out result) && result != null) { message.Importance = (int)emailMessage.Importance; } if (emailMessage.TryGetProperty(ItemSchema.Sensitivity, out result) && result != null) { message.Sensitivity = (int)emailMessage.Sensitivity; } if (emailMessage.TryGetProperty(ItemSchema.InReplyTo, out result) && result != null) { message.InReplyTo = emailMessage.InReplyTo; } if (emailMessage.TryGetProperty(ItemSchema.ConversationId, out result) && result != null) { message.ConversationId = emailMessage.ConversationId.ChangeKey; } if (emailMessage.TryGetProperty(ItemSchema.ReminderDueBy, out result) && result != null) { message.ReminderDueBy = emailMessage.ReminderDueBy; } if ( emailMessage.TryGetProperty(ExchangeHelper.PidTagFollowupIcon, out result) && result != null) { message.FlagIcon = ExchangeHelper.ConvertFlagIcon((int)result); } if (emailMessage.TryGetProperty(ItemSchema.DateTimeReceived, out result) && result != null) { message.ReceivedDateTime = emailMessage.DateTimeReceived; } if (emailMessage.TryGetProperty(ItemSchema.DateTimeSent, out result) && result != null) { message.SentDateTime = emailMessage.DateTimeSent; } if (emailMessage.TryGetProperty(ItemSchema.Flag, out result) && result != null) { message.FollowUpFlag = new FollowUpFlag() { StartDateTime = ((Flag)result).StartDate, DueDateTime = ((Flag)result).DueDate, CompleteDateTime = ((Flag)result).CompleteDate, Status = ExchangeHelper.ConvertFlagStatus(((Flag)result).FlagStatus), } } ; if (emailMessage.TryGetProperty(ItemSchema.Categories, out result) && result != null && emailMessage.Categories.Count > 0) { foreach (var category in emailMessage.Categories) { message.Categories.Add(category); } } if (emailMessage.ExtendedProperties != null) { foreach (var extendedProperty in emailMessage.ExtendedProperties) { if ( extendedProperty.PropertyDefinition.Equals( ExchangeHelper.MsgPropertyContentType)) { if (extendedProperty.Value.ToString().Contains("signed-data")) { message.IsEncrypted = true; } } } } NextReader.Process(message); SucceededMessageCount++; } catch (Exception e) { Logger.Error("Failed to load properties for message " + emailMessage.Id.UniqueId, e); FailedMessageCount++; } } } catch (Exception e) { Logger.Error("Failed to load properties for messages in " + exchangeFolder.FolderPath, e); FailedMessageCount += emails.Count; } ProcessedMessageCount += emails.Count; } if (more) { view.Offset += _pageSize; } } catch (Exception e) { Logger.Error("Failed to find results against folder " + exchangeFolder.FolderPath, e); more = false; } } } } catch (Exception e) { Logger.Error("Failed to run exporter", e); } finally { Close(); } }
private void RunImap() { try { Status = MessageProcessorStatus.Started; foreach (var mailbox in _folders) { if (Closed) { Logger.Warn("Cancellation requested"); break; } var folder = mailbox.FolderPath; var folderPath = folder.Name; var destinationFolder = mailbox.MappedDestination; try { var uids = _imapClient.Search(_searchCondition ?? SearchCondition.All(), folderPath); Logger.Info("Processing folder Name=" + folderPath + " Destination=" + destinationFolder + ", Count=" + folder.Messages + ", Unread=" + folder.Unread); var count = folder.Messages; var countQueued = 0; foreach (var uid in uids.Reverse()) { if (Closed) { Logger.Warn("Cancellation requested"); break; } if (_testOnly && count >= 20) { Logger.Debug("Testing only, hit 20 limit"); break; } try { var flags = _imapClient.GetMessageFlags(uid, folderPath).ToList(); if (flags.Contains(MessageFlag.Deleted)) { Logger.Debug("Skipping message " + folderPath + "/" + uid + ", its marked for deletion"); IgnoredMessageCount++; continue; } var message = GetImapMessage(_imapClient, folder, uid); message.Subject = Regex.Match(message.RawMessage, @"[\r\n]Subject:\s*(.*?)[\r\n]").Groups[1].Value; //Logger.Debug(folder + "/" + uid + "/" + subject + " " + String.Join(", ",flags)); Logger.Debug("Exporting " + uid + " from " + folder + " : " + message.Subject); if (!flags.Contains(MessageFlag.Seen)) { message.Flags.Add(MessageFlags.Unread); } if (flags.Contains(MessageFlag.Flagged)) { message.Flags.Add(MessageFlags.FollowUp); } message.SourceFolder = folderPath; message.DestinationFolder = destinationFolder; message.IsPublicFolder = mailbox.IsPublicFolder; NextReader.Process(message); SucceededMessageCount++; countQueued++; } catch (Exception ex) { Logger.Error("Failed to get and enqueue Imap message [" + uid + "]", ex); FailedMessageCount++; } ProcessedMessageCount++; } Logger.Info("Processed folder " + folderPath + ", read=" + count + ", queued=" + countQueued); } catch (Exception ex) { Logger.Error("Failed to select " + folderPath, ex); } } } catch (Exception e) { Logger.Error("Failed to run exporter"); } finally { Close(); } }