コード例 #1
0
        public void StreamingExchange()
        {
            try
            {
                Stopwatch watch;
                long elapsedMs = 0;

                watch = Stopwatch.StartNew();

                _logger.LogInformation(string.Format(LogsProcess.InitProcess, Variables.StreamingExchange, DateTime.Now.ToString()));

                Exchange.ExchangeService service = new Exchange.ExchangeService(Exchange.ExchangeVersion.Exchange2013);
                Exchange.WebCredentials wbcred = new Exchange.WebCredentials(
                    _parametersProcessum.GeracaoArquivoEmail.Login,
                    _parametersProcessum.GeracaoArquivoEmail.Senha);

                service.Credentials = wbcred;
                service.AutodiscoverUrl(_parametersProcessum.GeracaoArquivoEmail.Login, EWSConnection.RedirectionUrlValidationCallback);

                EWSConnection.SetStreamingNotifications(service, resetEvent, _parametersProcessum);

                resetEvent.WaitOne();

                watch.Stop();

                elapsedMs = watch.ElapsedMilliseconds;

                _logger.LogInformation(string.Format(LogsProcess.FinishProcess, Variables.StreamingExchange, DateTime.Now.ToString()));
                _logger.LogInformation(string.Format(LogsProcess.TimeExecution, Variables.StreamingExchange, elapsedMs.ToString()));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #2
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);
        }
コード例 #3
0
        private static void ProcessRogueMessages(EWSConnection ewsConnection)
        {
            Logger.InfoFormat("Processing rogue messages...");
            var folderName         = "Rogue Messages";
            var folders            = ewsConnection.Service.FindFolders(WellKnownFolderName.Inbox, new FolderView(100));
            var rogueMessageFolder = folders.SingleOrDefault(f => f.DisplayName == folderName);

            if (rogueMessageFolder == null)
            {
                Logger.InfoFormat("Could not find folder '{0}' on server. Using 'Deleted Items' instead.", folderName);
                rogueMessageFolder = Folder.Bind(ewsConnection.Service, WellKnownFolderName.DeletedItems);
            }

            var rogueMessages = ewsConnection.Router.RogueMessages;

            if (rogueMessages.Count == 0)
            {
                Logger.InfoFormat("No rogue messages found.");
                return;
            }

            Logger.InfoFormat("Replying to {0} rogue messages without configuration and moving to '{1}' folder...",
                              rogueMessages.Count, rogueMessageFolder.DisplayName);

            string[] previouslyProcessedRogueMessages;
            try
            {
                previouslyProcessedRogueMessages = ewsConnection.Service.FindItems(rogueMessageFolder.Id, new ItemView(1000)).
                                                   Select(i => new EWSIncomingMessage((EmailMessage)i).ConversationTopic).ToArray();
            }
            catch (Exception e)
            {
                Logger.ErrorFormat("Failed to connect to mailbox server. Will retry on next iteration. {0}", e);
                return;
            }

            foreach (var message in rogueMessages)
            {
                var ewsMessage = message as EWSIncomingMessage;
                if (ewsMessage == null)
                {
                    continue;
                }

                if (previouslyProcessedRogueMessages.Contains(ewsMessage.ConversationTopic))
                {
                    Logger.InfoFormat("Already replied to conversation topic.");
                    MoveMessageToFolder(ewsMessage, rogueMessageFolder);
                    continue;
                }

                var sender    = ewsMessage.SenderAlias;
                var recipient = "*****@*****.**";

                try
                {
                    Logger.InfoFormat("Replying to '{0}', subject '{1}'.", sender, ewsMessage.Subject);
                    var replyMessage = string.Format(ReplyMessage, sender, recipient);
                    ewsMessage.Reply(replyMessage, true);
                }
                catch (Exception e)
                {
                    Logger.ErrorFormat(
                        "Failed to send reply. Possible cause: original mail item moved by the Exchange Server rules. {0}",
                        e);
                }

                MoveMessageToFolder(ewsMessage, rogueMessageFolder);
            }

            Logger.InfoFormat("Completed replying to rogue messages.");
            rogueMessages.Clear();
        }
コード例 #4
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;
        }
コード例 #5
0
        private static void ProcessRogueMessages(EWSConnection ewsConnection)
        {
            Logger.InfoFormat("Processing rogue messages...");
            var folderName = "Rogue Messages";
            var folders = ewsConnection.Service.FindFolders(WellKnownFolderName.Inbox, new FolderView(100));
            var rogueMessageFolder = folders.SingleOrDefault(f => f.DisplayName == folderName);
            if (rogueMessageFolder == null)
            {
                Logger.InfoFormat("Could not find folder '{0}' on server. Using 'Deleted Items' instead.", folderName);
                rogueMessageFolder = Folder.Bind(ewsConnection.Service, WellKnownFolderName.DeletedItems);
            }

            var rogueMessages = ewsConnection.Router.RogueMessages;
            if (rogueMessages.Count == 0)
            {
                Logger.InfoFormat("No rogue messages found.");
                return;
            }

            Logger.InfoFormat("Replying to {0} rogue messages without configuration and moving to '{1}' folder...",
                rogueMessages.Count, rogueMessageFolder.DisplayName);

            string[] previouslyProcessedRogueMessages;
            try
            {
                previouslyProcessedRogueMessages = ewsConnection.Service.FindItems(rogueMessageFolder.Id, new ItemView(1000)).
                    Select(i => new EWSIncomingMessage((EmailMessage)i).ConversationTopic).ToArray();
            }
            catch (Exception e)
            {
                Logger.ErrorFormat("Failed to connect to mailbox server. Will retry on next iteration. {0}", e);
                return;
            }

            foreach (var message in rogueMessages)
            {
                var ewsMessage = message as EWSIncomingMessage;
                if (ewsMessage == null)
                {
                    continue;
                }

                if (previouslyProcessedRogueMessages.Contains(ewsMessage.ConversationTopic))
                {
                    Logger.InfoFormat("Already replied to conversation topic.");
                    MoveMessageToFolder(ewsMessage, rogueMessageFolder);
                    continue;
                }

                var sender = ewsMessage.SenderAlias;
                var recipient = "*****@*****.**";

                try
                {
                    Logger.InfoFormat("Replying to '{0}', subject '{1}'.", sender, ewsMessage.Subject);
                    var replyMessage = string.Format(ReplyMessage, sender, recipient);
                    ewsMessage.Reply(replyMessage, true);
                }
                catch (Exception e)
                {
                    Logger.ErrorFormat(
                        "Failed to send reply. Possible cause: original mail item moved by the Exchange Server rules. {0}",
                        e);
                }

                MoveMessageToFolder(ewsMessage, rogueMessageFolder);
            }

            Logger.InfoFormat("Completed replying to rogue messages.");
            rogueMessages.Clear();
        }