public RecipientsMailboxManager(RecipientsMailboxManagerRouter router, IEnumerable<string> recipients, IMessagePostProcessor postProcessor)
        {
            _router = router;
            _postProcessor = postProcessor;

            _clientId = _router.RegisterMailbox(m => ShouldConsiderMessage(m, recipients.ToArray()));
        }
Esempio n. 2
0
        public RecipientsMailboxManager(RecipientsMailboxManagerRouter router, IEnumerable <string> recipients, IMessagePostProcessor postProcessor)
        {
            _router        = router;
            _postProcessor = postProcessor;

            _clientId = _router.RegisterMailbox(m => ShouldConsiderMessage(m, recipients.ToArray()));
        }
Esempio n. 3
0
        private EWSConnection ConnectToEWS(Credentials credentials)
        {
            Logger.Debug($"Initializing FolderMailboxManager for email address {credentials.EmailAddress}");
            Stopwatch stopwatch = Stopwatch.StartNew();

            var exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1)
            {
                Credentials = new WebCredentials(credentials.UserName, credentials.Password),
                Timeout     = 60000
            };

            exchangeService.AutodiscoverUrl(
                credentials.EmailAddress,
                x =>
            {
                Logger.Debug($"Following redirection for EWS autodiscover: {x}");
                return(true);
            }
                );

            Logger.Debug($"Service URL: {exchangeService.Url}");

            EWSConnection ewsConnection = default(EWSConnection);

            try
            {
                EWSMailFolder ewsMailFolder           = new EWSMailFolder(Folder.Bind(exchangeService, WellKnownFolderName.Inbox));
                RecipientsMailboxManagerRouter router = new RecipientsMailboxManagerRouter(ewsMailFolder);

                ewsConnection = new EWSConnection
                {
                    Service = exchangeService,
                    Router  = router
                };
            }
            catch (Exception ex)
            {
                Logger.Error("Failed to connect to EWS", ex);
            }

            stopwatch.Stop();
            logger.LogSampledMetric("EWSConnectTime", stopwatch.ElapsedMilliseconds, "milliseconds");

            return(ewsConnection);
        }
        public void SingleMailboxBasicTest()
        {
            // Populate two messages, then register a client that accepts all messages
            // The expectation is that when we call GetMessages, we'll get exactly the messages we populated
            // for the input
            var inputMessages = CreateMessages(2, "Subject", DateTime.Now.AddDays(-1), TimeSpan.FromSeconds(1));
            var mock = new Mock<IMailFolder>();
            mock.Setup(x => x.GetMessages()).Returns(inputMessages);

            var router = new RecipientsMailboxManagerRouter(mock.Object);
            var id = router.RegisterMailbox(m => true);
            router.ProcessInbox();
            
            // Retrieve
            var output = router.GetMessages(id).ToArray();

            // Validate that it's the exact same set that we populated
            AssertMessageListsAreEqual(inputMessages, output);
        }
        public void SingleMailboxDetectNewMessagesTest()
        {
            // Populate some messages and retrieve them
            var inputMessages = CreateMessages(2, "Subject", DateTime.Now.AddDays(-1), TimeSpan.FromSeconds(1));
            var mock = new Mock<IMailFolder>();
            mock.Setup(x => x.GetMessages()).Returns(inputMessages);

            var router = new RecipientsMailboxManagerRouter(mock.Object);
            var id = router.RegisterMailbox(m => true);
            router.ProcessInbox();
            
            var output = router.GetMessages(id).ToArray();
            AssertMessageListsAreEqual(inputMessages, output);

            // Now populate a new message that is newer than the one we handled before, then call GetMessages again
            // and make sure it is retrieved
            var newMessages = CreateMessages(1, "Subject", DateTime.Now, TimeSpan.Zero);
            mock.Setup(x => x.GetMessages()).Returns(newMessages);
            router.ProcessInbox();

            output = router.GetMessages(id).ToArray();
            AssertMessageListsAreEqual(newMessages, output);
        }
Esempio n. 6
0
        private EWSConnection ConnectToEWS(Credentials credentials)
        {
            Logger.Debug($"Initializing FolderMailboxManager for email address {credentials.EmailAddress}");
            Stopwatch stopwatch = Stopwatch.StartNew();

            var exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1)
            {
                Credentials = new WebCredentials(credentials.UserName, credentials.Password),
                Timeout = 60000
            };

            exchangeService.AutodiscoverUrl(
                credentials.EmailAddress,
                x =>
                {
                    Logger.Debug($"Following redirection for EWS autodiscover: {x}");
                    return true;
                }
                );

            Logger.Debug($"Service URL: {exchangeService.Url}");

            EWSConnection ewsConnection = default(EWSConnection);
            try
            {
                EWSMailFolder ewsMailFolder = new EWSMailFolder(Folder.Bind(exchangeService, WellKnownFolderName.Inbox));
                RecipientsMailboxManagerRouter router = new RecipientsMailboxManagerRouter(ewsMailFolder);

                ewsConnection = new EWSConnection
                {
                    Service = exchangeService,
                    Router = router
                };
            }
            catch (Exception ex)
            {
                Logger.Error("Failed to connect to EWS", ex);
            }

            stopwatch.Stop();
            logger.LogSampledMetric("EWSConnectTime", stopwatch.ElapsedMilliseconds, "milliseconds");

            return ewsConnection;
        }
        public void ManyMailboxesTest()
        {
            const int numMailboxes = 100;
            var rand = new Random();

            var mock = new Mock<IMailFolder>();
            var router = new RecipientsMailboxManagerRouter(mock.Object);

            // Each client has a list of messages relevant to it, an ID, and a predicate
            var clients = new List<Tuple<IEnumerable<IIncomingEmailMessage>, int>>(numMailboxes);
            for (int i = 0; i < numMailboxes; i++)
            {
                var clientMessages = 
                    CreateMessages(
                        rand.Next(1, 10), 
                        string.Format("Subject_{0}", i), 
                        DateTime.Now.AddDays(-i), 
                        TimeSpan.FromSeconds(1));

                var id = router.RegisterMailbox(m => clientMessages.Any(message => message.Subject == m.Subject));

                clients.Add(new Tuple<IEnumerable<IIncomingEmailMessage>, int>(
                    clientMessages, 
                    id));
            }

            var messages = new List<IIncomingEmailMessage>();
            foreach (var client in clients)
            {
                messages.AddRange(client.Item1);
            }
            messages.AddRange(CreateMessages(rand.Next(1,100), "Dummy", DateTime.Now.AddDays(-3),TimeSpan.FromHours(3)));

            mock.Setup(x => x.GetMessages()).Returns(messages);

            router.ProcessInbox();

            foreach (var client in clients)
            {
                var output = router.GetMessages(client.Item2);
                AssertMessageListsAreEqual(client.Item1, output);
            }
        }
        public void OverlappingRecipientsTest()
        {
            // Populate some messages and retrieve them
            var inputMessages = CreateMessages(100, "Subject", DateTime.Now.AddDays(-1), TimeSpan.FromSeconds(1));
            var mock = new Mock<IMailFolder>();
            mock.Setup(x => x.GetMessages()).Returns(inputMessages);

            // Set the timeout threshold to 0, so that we always re-process the messages in the folder, even if there
            // are no new items
            var router = new RecipientsMailboxManagerRouter(mock.Object);
            
            var client1UniqueMessage = inputMessages[4];
            var client2UniqueMessage = inputMessages[7];
            var sharedMessage = inputMessages[1];
            
            var client1Messages = new List<IIncomingEmailMessage> { sharedMessage, client1UniqueMessage };
            var client2Messages = new List<IIncomingEmailMessage> { sharedMessage, client2UniqueMessage };
            var client1Id = router.RegisterMailbox(
                m => { return client1Messages.Any(message => message.Subject == m.Subject); });
            var client2Id = router.RegisterMailbox(
                m => { return client2Messages.Any(message => message.Subject == m.Subject); });

            router.ProcessInbox();

            var client1Output = router.GetMessages(client1Id).ToArray();
            var client2Output = router.GetMessages(client2Id).ToArray();

            // Make sure that each client gets the message unique for them
            Assert.AreEqual(1, client1Output.Count(m => m.Subject == client1UniqueMessage.Subject));
            Assert.AreEqual(1, client2Output.Count(m => m.Subject == client2UniqueMessage.Subject));

            // The shared message should only appear once (in either of them)
            Assert.AreEqual(1, client1Output.Concat(client2Output).Count(m => m.Subject == sharedMessage.Subject));

            // There shouldn't be any other messages, so total messages should be 3
            Assert.AreEqual(3, client1Output.Concat(client2Output).Count());
        }
        public void TwoMailboxesBasicTest()
        {
            // Populate some messages and retrieve them
            var inputMessages = CreateMessages(100, "Subject", DateTime.Now.AddDays(-1), TimeSpan.FromSeconds(1));
            var mock = new Mock<IMailFolder>();
            mock.Setup(x => x.GetMessages()).Returns(inputMessages);

            // Set the timeout threshold to 0, so that we always re-process the messages in the folder, even if there
            // are no new items
            var router = new RecipientsMailboxManagerRouter(mock.Object);
            var client1Messages = new List<IIncomingEmailMessage>{inputMessages[1], inputMessages[4]};
            var client2Messages = new List<IIncomingEmailMessage>{inputMessages[8], inputMessages[7]};
            
            var client1Id = router.RegisterMailbox( 
                m => client1Messages.Any(message => message.Subject == m.Subject) );
            
            var client2Id = router.RegisterMailbox(
                m => client2Messages.Any(message => message.Subject == m.Subject) );

            router.ProcessInbox();

            var client1Output = router.GetMessages(client1Id).ToArray();
            var client2Output = router.GetMessages(client2Id).ToArray();
            AssertMessageListsAreEqual(client1Messages, client1Output);
            AssertMessageListsAreEqual(client2Messages, client2Output);
        }
        public void SingleMailboxWIthIgnoredMessagesTest()
        {
            // Populate some messages 
            var inputMessages = CreateMessages(10, "Subject", DateTime.Now.AddDays(-1), TimeSpan.FromSeconds(1));
            var mock = new Mock<IMailFolder>();
            mock.Setup(x => x.GetMessages()).Returns(inputMessages);

            // Only one of the messages is relevant for the client
            var relevantMessages = new List<IIncomingEmailMessage> { inputMessages[3] };

            var router = new RecipientsMailboxManagerRouter(mock.Object);
            var id = router.RegisterMailbox(m => relevantMessages.Any(message => message.Subject == m.Subject));
            router.ProcessInbox();

            var output = router.GetMessages(id).ToArray();
            AssertMessageListsAreEqual(relevantMessages, output);
        }