Example #1
0
        public IList <EmailMessageWrapper> FindUnreadEmails(ExchangeConfigurationCredentials credentials)
        {
            if (credentials == null)
            {
                throw new ArgumentNullException("credentials");
            }

            var itemView = new ItemView(MaxItemCount);

            itemView.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);

            var outOffOfficeEmailFilter = new SearchFilter.IsNotEqualTo(ItemSchema.ItemClass, "IPM.Note.Rules.OofTemplate.Microsoft");
            var filters = new SearchFilter.SearchFilterCollection(LogicalOperator.And)
            {
                UnreadEmailFilter,
                outOffOfficeEmailFilter
            };
            var folderId = GetInboxFolder(credentials);

            var result = _exchangeService.FindItems(credentials, folderId, filters, itemView)
                         .Select(message => GetEmailMessageWrapper(message, credentials.Id))
                         .ToList();

            if (result.Any(c => c.Address == null || c.DisplayName == null || c.ExchangeConfigurationId == null || c.EmlContent == null || c.ItemId == null))
            {
                System.Diagnostics.Debug.WriteLine($"{result.Count} unread email(s) found in mailbox {credentials.ExchangeName}");
            }
            return(result);
        }
        //public ExchangeServiceWrapper()
        //{
        //    _traceEnabled = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["EnableExchangeTracing"]);

        //    if (_traceEnabled)
        //    {
        //        _traceListener = new TextTraceListener(System.Configuration.ConfigurationManager.AppSettings["ExchangeTracePath"]);
        //    }
        //}

        public IList <EmailMessage> FindItems(ExchangeConfigurationCredentials credentials, FolderId folderId, SearchFilter.SearchFilterCollection filter, ItemView itemView)
        {
            var client = GetClient(credentials);

            client.Timeout = 900000; // 15 minutes in milliseconds

            return(client.FindItems(folderId, filter, itemView)
                   .Cast <EmailMessage>()
                   .ToList());
        }
Example #3
0
        private static ExchangeConfigurationCredentials GetCredential()
        {
            var credential = new ExchangeConfigurationCredentials
            {
                Id            = Guid.NewGuid(), //e04dfecb-4da0-4b39-a238-f7bd81e32681
                ConnectionUrl = "https://outlook.office365.com/EWS/Exchange.asmx",
                Domain        = "ad001.siemens.net",
                ExchangeName  = "TestExchangeConection",
                Password      = "******", // "Lqy0bB05wni612a0XBMyiQ==",//EncryptString("saltedCarame8"),
                Username      = "******",
            };

            return(credential);
        }
Example #4
0
        private static FolderId GetFolder(ExchangeConfigurationCredentials credentials, WellKnownFolderName folder)
        {
            FolderId result;
            var      emailAddressValidator = new EmailAddressAttribute();

            if (emailAddressValidator.IsValid(credentials.ExchangeName))
            {
                var mailBox = new Mailbox(credentials.ExchangeName);
                result = new FolderId(folder, mailBox);
            }
            else
            {
                result = new FolderId(folder);
            }
            return(result);
        }
        public ExchangeService GetClient(ExchangeConfigurationCredentials credentials)
        {
            if (credentials == null)
            {
                throw new ArgumentNullException("credentials");
            }

            //ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;


            var client = new ExchangeService(ExchangeServerVersion)
            {
                Url = new Uri(credentials.ConnectionUrl, UriKind.Absolute)
            };


            if (true)
            {
                client.TraceEnabled  = true;
                client.TraceListener = new TextTraceListener("C:\\Temp\\Globomantics\\exchangeTrace.txt");;
            }

            //ServicePointManager.Expect100Continue = true;
            //ServicePointManager.SecurityProtocol =
            //        SecurityProtocolType.Tls12;

            var proxy = new WebProxy();

            proxy.UseDefaultCredentials = false;
            proxy.BypassProxyOnLocal    = true;
            proxy.Address    = new Uri("http://de.coia.siemens.net:9400");
            proxy.BypassList = new[] { "webmail-emea.siemens.net", "mail-de.siemens.net" };
            client.WebProxy  = proxy;

            client.UseDefaultCredentials = true;
            client.PreAuthenticate       = false;

            client.Credentials = new WebCredentials(credentials.Username
                                                    , credentials.Password
                                                    , credentials.Domain);

            return(client);
        }
Example #6
0
        public void ReadEmails(ExchangeConfigurationCredentials credentials,
                               IList <MyEmailMessage> emailMessages,
                               Guid exchangeConfigurationId)
        {
            if (credentials == null)
            {
                throw new ArgumentNullException("credentials");
            }
            if (emailMessages == null)
            {
                throw new ArgumentNullException("emailMessages");
            }
            //if (emailMessageWriteRepository == null) throw new ArgumentNullException("emailMessageWriteRepository");

            if (emailMessages.Any())
            {
                var exchangeService = _exchangeService.GetClient(credentials);
                foreach (var email in emailMessages)
                {
                    var exchangeEmail = GetUnreadEmail(email.ItemId, exchangeService);
                    try
                    {
                        //emailMessageWriteRepository.Save(email);
                        MarkEmail(exchangeEmail, true);
                        if (exchangeEmail.Id != null && exchangeEmail.Id.UniqueId != null)
                        {
                            System.Diagnostics.Debug.WriteLine(
                                $"Saved email in DB. Id: {{0}}",
                                exchangeEmail.Id.UniqueId);
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine($"An error happened while setting email {email.Id} to read. {ex}");

                        MarkEmail(exchangeEmail, false);

                        throw;
                    }
                }
            }
        }
Example #7
0
 public MailboxPolling(IExchangeServiceClient exchangeServiceClient)
 {
     _exchangeServiceClient   = exchangeServiceClient;
     this.Credentials         = GetCredential();
     _exchangeConfigurationId = new Guid();
 }
Example #8
0
 private static FolderId GetInboxFolder(ExchangeConfigurationCredentials credentials)
 {
     return(GetFolder(credentials, WellKnownFolderName.Inbox));
 }
 public EmailMessage GetEmailMessage(ExchangeConfigurationCredentials credentials)
 {
     return(new EmailMessage(GetClient(credentials)));
 }