public override void Initialise(List <MailFolder> folderList) { Status = MessageProcessorStatus.Initialising; _mainFolderList = folderList; service = ExchangeHelper.ExchangeConnect(_hostname, _username, _password); //service.TraceEnabled = true; //service.TraceFlags = TraceFlags.All; folders = new List <ExchangeFolder>(); ExchangeHelper.GetAllSubFolders(service, new ExchangeFolder { Folder = Folder.Bind(service, WellKnownFolderName.MsgFolderRoot) }, folders, false); if (_createAllFolders) { // we need to create all folders which don't exist foreach (var mailFolder in _mainFolderList) { GetCreateFolder(mailFolder.DestinationFolder); } } _lastState = new ImportState(); _queue = new PCQueue <RawMessageDescriptor, ImportState>(Name + "-exchangeTarget") { ProduceMethod = ProcessMessage, InitialiseProducer = () => _lastState, ShutdownProducer = ShutdownQueue }; _queue.Start(); Status = MessageProcessorStatus.Initialised; }
public override void Initialise(List <MailFolder> folderList) { Status = MessageProcessorStatus.Initialising; service = ExchangeHelper.ExchangeConnect(_hostname, _username, _password); folders = new List <ExchangeFolder>(); // Use Exchange Helper to get all the folders for this account ExchangeHelper.GetAllSubFolders(service, new ExchangeFolder() { Folder = Folder.Bind(service, WellKnownFolderName.MsgFolderRoot) }, folders, false); if (IncludePublicFolders) { Logger.Debug("Including Public Folders"); ExchangeHelper.GetAllSubFolders(service, new ExchangeFolder() { Folder = Folder.Bind(service, WellKnownFolderName.PublicFoldersRoot), IsPublicFolder = true }, folders, false); } // Are we limited folders to a specific list? if (_limitFolderList != null) { var newFolders = new List <ExchangeFolder>(); foreach (var mailbox in _limitFolderList) { var mailboxMatch = mailbox.ToLower().Replace('/', '\\');; newFolders.AddRange(folders.Where(folder => folder.FolderPath.ToLower().Equals(mailboxMatch))); } folders = newFolders; } // Scan the folders to get message counts ExchangeHelper.GetFolderSummary(service, folders, _startDate, _endDate); folders.ForEach(folder => TotalMessages += !TestOnly ? folder.MessageCount : (folder.MessageCount > 20 ? 20 : folder.MessageCount)); Logger.Debug("Found " + folders.Count + " folders and " + TotalMessages + " messages."); // Now build the folder list that we pass on to the next folders. foreach (var exchangeFolder in folders) { var folder = new MailFolder() { SourceFolder = exchangeFolder.FolderPath, DestinationFolder = exchangeFolder.MappedDestination, MessageCount = exchangeFolder.MessageCount, }; _mainFolderList.Add(folder); } // Now initialise the next read, I am not going to start reading unless I know the pipeline is groovy NextReader.Initialise(_mainFolderList); Status = MessageProcessorStatus.Initialised; Logger.Info("ExchangeExporter Initialised"); }
private FolderId GetCreateFolder(string destinationFolder, bool secondAttempt = false) { lock (_folderCreationLock) { if (!folders.Any(folder => folder.FolderPath.Equals(destinationFolder))) { // folder doesn't exist // getCreate its parent var parentPath = Regex.Replace(destinationFolder, @"\\[^\\]+$", ""); FolderId parentFolderId = null; if (parentPath.Equals(destinationFolder)) { // we are at the root parentPath = ""; parentFolderId = WellKnownFolderName.MsgFolderRoot; } else { parentFolderId = GetCreateFolder(parentPath); } Logger.Debug("Folder " + destinationFolder + " doesn't exist, creating."); var destinationFolderName = Regex.Replace(destinationFolder, @"^.*\\", ""); Folder folder = new Folder(service) { DisplayName = destinationFolderName }; try { folder.Save(parentFolderId); } catch (Exception e) { // If the folder exists, we need to refresh and have another crack if (e.Message.Equals("A folder with the specified name already exists.") && !secondAttempt) { Logger.Warn("Looks like the folder " + destinationFolder + " was created under our feet, refreshing the folder list. We will only attempt this once per folder."); // Oops, the folders have been updated on the server, we need a refresh var newFolders = new List <ExchangeFolder>(); ExchangeHelper.GetAllSubFolders(service, new ExchangeFolder { Folder = Folder.Bind(service, WellKnownFolderName.MsgFolderRoot) }, newFolders, false); folders = newFolders; // lets try again return(GetCreateFolder(destinationFolder, true)); } throw e; } folders.Add(new ExchangeFolder() { Folder = folder, FolderId = folder.Id, FolderPath = String.IsNullOrEmpty(parentPath) ? destinationFolderName : parentPath + @"\" + destinationFolderName, }); return(folder.Id); } else { return(folders.First(folder => folder.FolderPath.Equals(destinationFolder)).FolderId); } } }