Exemplo n.º 1
0
        private void SyncMessage(Account account, string mailboxName)
        {
            int abort = 0;

            if (abortLatches.TryGetValue(account.AccountName, out abort) && abort == 1)
            {
                // Abort sync.
                return;
            }

            ImapClient imapClient = new ImapClient(account);

            if (!imapClient.Connect())
            {
                Trace.WriteLine(imapClient.Error);
                return;
            }

            ExamineResult status;
            bool          readOnly = true;

            if (!imapClient.SelectMailbox(mailboxName, readOnly, out status))
            {
                Debug.WriteLine("MessageManager.SyncMessage(): Unable to select mailbox " + mailboxName + ".\n" + imapClient.Error);
                imapClient.Disconnect();
                return;
            }

            int localLargestUid = DatabaseManager.GetMaxUid(account.AccountName, mailboxName);

            // Download the recent message headers that have never been downloaded yet.
            int serverLargestUid = status.UidNext - 1;

            if (serverLargestUid > localLargestUid)
            {
                SyncNewMessageHeaders(account, mailboxName, imapClient, localLargestUid + 1, serverLargestUid);
            }

            // Sync changes to the existing message headers (flags, etc.).
            if (localLargestUid > 0)
            {
                SyncExistingMessageHeaders(account, mailboxName, imapClient, 1, localLargestUid);
            }

            imapClient.Disconnect();
        }
        private void SetMessage(Message newMessage)
        {
            if (Message != null)
            {
                Message.PropertyChanged -= HandleModelPropertyChanged;
            }

            Message = newMessage;
            Message.PropertyChanged += HandleModelPropertyChanged;

            Date = ImapParser.ParseDate(Message.DateString);

            if (string.IsNullOrEmpty(Message.Body))
            {
                DownloadMessageBodyAsync();
            }
            else
            {
                // Loading a message whose body has already been downloaded.
                TextBody = MimeUtility.GetTextBody(Message.Body);
                HtmlBody = MimeUtility.GetHtmlBody(Message.Body);
                ProcessedHtmlBody = ProcessHtmlBody(HtmlBody);
                LoadAttachments(Message.Body);

                if (!Message.IsSeen)
                {
                    // Set the \Seen flag.
                    Message.IsSeen = true;
                    DatabaseManager.Update(Message);

                    Task.Run(() => {
                        ImapClient imap = new ImapClient(notification.SelectedAccount);
                        if (imap.Connect())
                        {
                            // Not readonly because we need to set the \Seen flag.
                            bool readOnly = false;
                            if (imap.SelectMailbox(notification.SelectedMailbox.DirectoryPath, readOnly))
                            {
                                imap.SetFlag(ImapClient.FlagAction.Add, Message.Uid, "\\Seen");
                            }

                            imap.Disconnect();
                        }
                    });
                }
            }
        }
        private async void DownloadMessageBodyAsync()
        {
            Loading = true;

            string msgBody = string.Empty;

            await Task.Run(() => {
                ImapClient imap = new ImapClient(notification.SelectedAccount);
                if (imap.Connect())
                {
                    // Not readonly because we need to set the \Seen flag.
                    bool readOnly = false;
                    if (imap.SelectMailbox(notification.SelectedMailbox.DirectoryPath, readOnly))
                    {
                        msgBody = imap.FetchBody(Message.Uid);
                    }

                    imap.Disconnect();

                    if (!Message.IsSeen)
                    {
                        Message.IsSeen = true;

                        // The server should automatically set the \Seen flag when BODY is fetched.
                        // We shouldn't have to send command for this.
                    }
                }
            });

            if (msgBody != string.Empty)
            {
                Message.Body = msgBody;
            }

            // Store the Body and IsSeen flag to the database.
            DatabaseManager.Update(Message);

            Loading = false;
        }
        private void SyncMessage(Account account, string mailboxName)
        {
            int abort = 0;
            if (abortLatches.TryGetValue(account.AccountName, out abort) && abort == 1)
            {
                // Abort sync.
                return;
            }

            ImapClient imapClient = new ImapClient(account);
            if (!imapClient.Connect())
            {
                Trace.WriteLine(imapClient.Error);
                return;
            }

            ExamineResult status;
            bool readOnly = true;
            if (!imapClient.SelectMailbox(mailboxName, readOnly, out status))
            {
                Debug.WriteLine("MessageManager.SyncMessage(): Unable to select mailbox " + mailboxName + ".\n" + imapClient.Error);
                imapClient.Disconnect();
                return;
            }

            int localLargestUid = DatabaseManager.GetMaxUid(account.AccountName, mailboxName);

            // Download the recent message headers that have never been downloaded yet.
            int serverLargestUid = status.UidNext - 1;
            if (serverLargestUid > localLargestUid)
            {
                SyncNewMessageHeaders(account, mailboxName, imapClient, localLargestUid + 1, serverLargestUid);
            }

            // Sync changes to the existing message headers (flags, etc.).
            if (localLargestUid > 0)
            {
                SyncExistingMessageHeaders(account, mailboxName, imapClient, 1, localLargestUid);
            }

            imapClient.Disconnect();
        }