Beispiel #1
0
        private void SubscribeNotificationsThread()
        {
            ExchangeTraceListener trace   = new ExchangeTraceListener();
            ExchangeService       service = new ExchangeService
            {
                TraceListener = trace,
                TraceFlags    = TraceFlags.AutodiscoverConfiguration,
                TraceEnabled  = true,
                Credentials   = new WebCredentials(UserSettings.Email.EmailAddress, UserSettings.Email.EmailPassword),
                Url           = exServiceUri
            };

            subconn = new StreamingSubscriptionConnection(service, 30);

            do
            {
                try
                {
                    StreamingSubscription streamingSubscription = service.SubscribeToStreamingNotificationsOnAllFolders(EventType.NewMail);

                    subconn.AddSubscription(streamingSubscription);
                    subconn.OnNotificationEvent += Connection_OnNotificationEvent;
                    subconn.OnSubscriptionError += Connection_OnSubscriptionError;
                    subconn.OnDisconnect        += Connection_OnDisconnect;
                    subconn.Open();
                }
                catch
                {
                    if (trace.Result == ETraceResult.LoginError)
                    {
                        Status = EProviderStatus.LoginError;
                        return;
                    }
                    else
                    {
                        Wait(ApplicationSettings.General.WaitForNextConnectionRetry);
                    }
                }
            } while ((subconn == null || !subconn.IsOpen) && !exitToken.IsCancellationRequested);
        }
Beispiel #2
0
        private void MainThread()
        {
            ExchangeTraceListener trace = new ExchangeTraceListener();

            exService = new ExchangeService();
            exService.TraceListener = trace;
            exService.TraceFlags    = TraceFlags.AutodiscoverConfiguration;
            exService.TraceEnabled  = true;

            do
            {
                try
                {
                    exService.Credentials = new WebCredentials(UserSettings.Email.EmailAddress, UserSettings.Email.EmailPassword);
                    //exService.UseDefaultCredentials = true;
                    exService.AutodiscoverUrl(UserSettings.Email.EmailAddress, (string redirectionUrl) =>
                    {
                        // The default for the validation callback is to reject the URL.
                        bool result        = false;
                        Uri redirectionUri = new Uri(redirectionUrl);

                        // Validate the contents of the redirection URL. In this simple validation
                        // callback, the redirection URL is considered valid if it is using HTTPS
                        // to encrypt the authentication credentials.
                        if (redirectionUri.Scheme == "https")
                        {
                            result = true;
                        }

                        return(result);
                    });
                }
                catch
                {
                    if (trace.Result == ETraceResult.LoginError)
                    {
                        Status = EProviderStatus.LoginError;
                        return;
                    }
                    else
                    {
                        Wait(ApplicationSettings.General.WaitForNextConnectionRetry);
                    }
                }
            } while (exService.Url == null && !exitToken.IsCancellationRequested);

            if (exitToken.IsCancellationRequested)
            {
                return;
            }

            Status = EProviderStatus.Connecting;

            exServiceUri = exService.Url;

            if ((emailSenderThread == null || !emailSenderThread.IsAlive) && !exitToken.IsCancellationRequested)
            {
                emailSenderThread              = new Thread(EmailSenderThread);
                emailSenderThread.Name         = "Email Sender";
                emailSenderThread.IsBackground = true;
                emailSenderThread.Start();
            }

            if ((subscribeThread == null || !subscribeThread.IsAlive) && !exitToken.IsCancellationRequested)
            {
                subscribeThread              = new Thread(SubscribeNotificationsThread);
                subscribeThread.Name         = "Exchange Subscription Thread";
                subscribeThread.IsBackground = true;
                subscribeThread.Start();
            }

            if ((syncThread == null || !syncThread.IsAlive) && !exitToken.IsCancellationRequested)
            {
                syncThread              = new Thread(ExchangeSync);
                syncThread.Name         = "Exchange Sync";
                syncThread.IsBackground = true;
                syncThread.Start();
            }
        }
Beispiel #3
0
        private void ExchangeSync()
        {
            ExchangeTraceListener trace   = new ExchangeTraceListener();
            ExchangeService       service = new ExchangeService
            {
                TraceListener = trace,
                TraceFlags    = TraceFlags.AutodiscoverConfiguration,
                TraceEnabled  = true,
                Credentials   = new WebCredentials(UserSettings.Email.EmailAddress, UserSettings.Email.EmailPassword),
                Url           = exServiceUri
            };

            bool IsSynced = false;

            Status = EProviderStatus.Syncronizing;

            do
            {
                try
                {
                    ItemView itemView = new ItemView(int.MaxValue)
                    {
                        PropertySet = new PropertySet(BasePropertySet.IdOnly)
                    };
                    FolderView folderView = new FolderView(int.MaxValue)
                    {
                        PropertySet = new PropertySet(BasePropertySet.IdOnly), Traversal = FolderTraversal.Deep
                    };
                    folderView.PropertySet.Add(FolderSchema.WellKnownFolderName);

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

                    // try to get last week messages (high priority)
                    foreach (Item item in FindItemsInSubfolders(service, new FolderId(WellKnownFolderName.MsgFolderRoot), "from:fdl received:>=lastweek", folderView, itemView))
                    {
                        if (exitToken.IsCancellationRequested)
                        {
                            break;
                        }

                        if (!(item is EmailMessage))
                        {
                            continue;
                        }

                        EmailMessage message = EmailMessage.Bind(service, item.Id);
                        NotifyNewMessage(message);
                    }

                    // then all the other messages
                    foreach (Item item in FindItemsInSubfolders(service, new FolderId(WellKnownFolderName.MsgFolderRoot), "from:fdl", folderView, itemView))
                    {
                        if (exitToken.IsCancellationRequested)
                        {
                            break;
                        }

                        if (!(item is EmailMessage))
                        {
                            continue;
                        }

                        EmailMessage message = EmailMessage.Bind(service, item.Id);
                        NotifyNewMessage(message);
                    }

                    IsSynced = true;
                    Status   = EProviderStatus.Syncronized;
                }
                catch
                {
                    if (trace.Result == ETraceResult.LoginError)
                    {
                        Status = EProviderStatus.LoginError;
                        return;
                    }
                    else
                    {
                        Wait(ApplicationSettings.General.WaitForNextConnectionRetry);
                    }
                }
            } while (!IsSynced && !exitToken.IsCancellationRequested);
        }
Beispiel #4
0
        private void EmailSenderThread()
        {
            ExchangeTraceListener trace   = new ExchangeTraceListener();
            ExchangeService       service = new ExchangeService
            {
                TraceListener = trace,
                TraceFlags    = TraceFlags.AutodiscoverConfiguration,
                TraceEnabled  = true,
                Credentials   = new WebCredentials(UserSettings.Email.EmailAddress, UserSettings.Email.EmailPassword),
                Url           = exServiceUri
            };

            while (!exitToken.IsCancellationRequested)
            {
                while (!emailQueue.IsEmpty)
                {
                    EmailMessageDTO message;
                    bool            IsSent = false;

                    if (!emailQueue.TryDequeue(out message))
                    {
                        continue;
                    }

                    do
                    {
                        try
                        {
                            EmailMessage msg = new EmailMessage(service);

                            msg.Subject    = message.Subject;
                            msg.Body       = message.Body;
                            msg.Importance = message.Importance;

                            msg.ToRecipients.AddRange(message.ToRecipients);
                            msg.CcRecipients.AddRange(message.CcRecipients);

                            foreach (string file in message.Attachments)
                            {
                                msg.Attachments.AddFileAttachment(file);
                            }

                            msg.SendAndSaveCopy();
                            IsSent = true;

                            NotifyMessageSent(message);
                        }
                        catch
                        {
                            if (trace.Result == ETraceResult.LoginError)
                            {
                                Status = EProviderStatus.LoginError;
                                return;
                            }
                            else
                            {
                                Wait(ApplicationSettings.General.WaitForNextConnectionRetry);
                            }
                        }
                    }while (!IsSent);
                }

                Wait(ApplicationSettings.General.WaitForNextEmailCheck);
            }
        }