Ejemplo n.º 1
0
        /*
         * public static bool SetProperty(EmailMessage message, PropertyDefinition propertyDefinition, object value)
         * {
         *  if (message == null)
         *      return false;
         *  // get value of PropertyBag property — that is wrapper
         *  // over dictionary of inner message’s properties
         *  var members = message.GetType().FindMembers(MemberTypes.Property, BindingFlags.NonPublic | BindingFlags.Instance, PartialName, "PropertyBag");
         *  if (members.Length < 1)
         *      return false;
         *
         *  var propertyInfo = members[0] as PropertyInfo;
         *  if (propertyInfo == null)
         *      return false;
         *
         *  var bag = propertyInfo.GetValue(message, null);
         *  members = bag.GetType().FindMembers(MemberTypes.Property, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, PartialName, "Properties");
         *
         *  if (members.Length < 1)
         *      return false;
         *
         *  // get dictionary of properties values
         *  var properties = ((PropertyInfo)members[0]).GetMethod.Invoke(bag, null);
         *  var dictionary = properties as Dictionary<PropertyDefinition, object>;
         *  if (dictionary == null)
         *      return false;
         *  dictionary[propertyDefinition] = value;
         *
         *  return true;
         * }
         */

        // Get a summary of all the folders
        public static void GetFolderSummary(ExchangeService service, List <ExchangeFolder> folderStore, DateTime startDate, DateTime endDate, bool purgeIgnored = true)
        {
            SearchFilter.SearchFilterCollection filter = new SearchFilter.SearchFilterCollection();
            filter.LogicalOperator = LogicalOperator.And;
            Logger.Debug("Getting mails from " + startDate + " to " + endDate);
            filter.Add(new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, startDate));
            filter.Add(new SearchFilter.IsLessThanOrEqualTo(ItemSchema.DateTimeReceived, endDate));
            var view = new ItemView(20, 0, OffsetBasePoint.Beginning)
            {
                PropertySet = PropertySet.IdOnly
            };
            var ignoredFolders = new List <ExchangeFolder>();

            foreach (var exchangeFolder in folderStore)
            {
                var destinationFolder = FolderMapping.ApplyMappings(exchangeFolder.FolderPath, MailProvider.Exchange);
                if (!String.IsNullOrWhiteSpace(destinationFolder))
                {
                    exchangeFolder.MappedDestination = destinationFolder;
                    var findResults = service.FindItems(exchangeFolder.FolderId, filter, view);
                    Logger.Debug(exchangeFolder.FolderPath + " => " + exchangeFolder.MappedDestination + ", " +
                                 findResults.TotalCount + " messages.");
                    exchangeFolder.MessageCount = findResults.TotalCount;
                }
                else
                {
                    ignoredFolders.Add(exchangeFolder);
                }
            }
            if (purgeIgnored)
            {
                foreach (var exchangeFolder in ignoredFolders)
                {
                    folderStore.Remove(exchangeFolder);
                }
            }
        }
Ejemplo n.º 2
0
 public override void Initialise(List <MailFolder> folderList)
 {
     Status   = MessageProcessorStatus.Initialising;
     _folders = new List <ImapFolder>();
     try
     {
         _imapClient = new ImapClient(_server, _useSsl ? 993 : 143, _username, _password, AuthMethod.Login, _useSsl,
                                      delegate { return(true); });
         Logger.Debug("Logged into " + _server + " as " + _username);
         var folders = _imapClient.ListMailboxes();
         if (_startDate != null || _endDate != null)
         {
             // ok we need to do a search
             if (_startDate != null)
             {
                 _searchCondition = SearchCondition.Since((DateTime)_startDate);
             }
             if (_endDate != null)
             {
                 _searchCondition = _searchCondition == null
                     ? SearchCondition.Before((DateTime)_endDate)
                     : _searchCondition.And(SearchCondition.Before((DateTime)_endDate));
             }
             Logger.Debug("Only getting messages " + _searchCondition);
         }
         // Are we limiting the folder list?
         if (_limitFolderList != null)
         {
             var newFolders = new List <String>();
             foreach (var mailbox in _limitFolderList)
             {
                 var mailboxMatch = mailbox.ToLower().Replace('\\', '/');;
                 newFolders.AddRange(folders.Where(folder => folder.ToLower().Equals(mailboxMatch)));
             }
             folders = newFolders;
         }
         foreach (var folderPath in folders)
         {
             bool isPublicFolder    = false;
             var  destinationFolder = FolderMapping.ApplyMappings(folderPath, Provider);
             if (IncludePublicFolders && (String.Equals(destinationFolder, PublicFolderRoot) || destinationFolder.StartsWith(PublicFolderRoot + @"\")))
             {
                 isPublicFolder = true;
                 var start = PublicFolderRoot.Length + (destinationFolder.StartsWith(PublicFolderRoot + @"\") ? 1 : 0);
                 destinationFolder = destinationFolder.Substring(start, destinationFolder.Length - start);
             }
             if (!String.IsNullOrWhiteSpace(destinationFolder))
             {
                 try
                 {
                     var folder = _imapClient.GetMailboxInfo(folderPath);
                     if (folder.Messages == 0)
                     {
                         Logger.Debug("Skipping folder " + folderPath + ", no messages at all.");
                         continue;
                     }
                     int messageCount = 0;
                     if (_searchCondition != null)
                     {
                         var uids = _imapClient.Search(_searchCondition, folderPath);
                         messageCount = uids.Count();
                     }
                     else
                     {
                         messageCount = folder.Messages;
                     }
                     // Add it to our main folder list
                     _mainFolderList.Add(new MailFolder()
                     {
                         DestinationFolder = destinationFolder,
                         MessageCount      = FailedMessageCount,
                         SourceFolder      = folderPath
                     });
                     if (messageCount == 0)
                     {
                         Logger.Debug("Skipping folder " + folderPath + ", no messages within criteria.");
                         continue;
                     }
                     _folders.Add(new ImapFolder()
                     {
                         MappedDestination = destinationFolder,
                         FolderPath        = folder,
                         IsPublicFolder    = isPublicFolder
                     });
                     TotalMessages += !_testOnly ? messageCount : (messageCount > 20 ? 20 : messageCount);
                     Logger.Debug("Will process " + folderPath + " => " + (isPublicFolder ? "[PUBLIC FOLDER]/" : "") + destinationFolder + ", " + messageCount + " messages, " + TotalMessages + " messages total so far.");
                 }
                 catch (Exception ex)
                 {
                     Logger.Error("Failed to get Mailbox " + folderPath + ", skipping.", ex);
                 }
             }
             else
             {
                 Logger.Info("Ignoring folder " + folderPath + ", no destination specified.");
             }
         }
     }
     catch (InvalidCredentialsException ex)
     {
         Logger.Error("Imap Runner for " + _username + " [********] to " + _server + " failed : " + ex.Message, ex);
         throw new MessageProcessorException("Imap Runner for " + _username + " [********] to " + _server + " failed : " + ex.Message)
               {
                   Status = MessageProcessorStatus.SourceAuthFailure
               };
     }
     catch (SocketException ex)
     {
         Logger.Error("Imap Runner for " + _username + " [********] to " + _server + " failed : " + ex.Message, ex);
         throw new MessageProcessorException("Imap Runner for " + _username + " [********] to " + _server + " failed : " + ex.Message)
               {
                   Status = MessageProcessorStatus.ConnectionError
               };
     }
     NextReader.Initialise(_mainFolderList);
     Status = MessageProcessorStatus.Initialised;
     Logger.Info("ExchangeExporter Initialised");
 }