Exemple #1
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");
 }