Ejemplo n.º 1
0
        public MailMessageEx Top(int messageId, int lineCount)
        {
            if (messageId < 1)
            {
                throw new ArgumentOutOfRangeException("messageId");
            }

            if (lineCount < 0)
            {
                throw new ArgumentOutOfRangeException("lineCount");
            }

            RetrResponse response;

            using (TopCommand command = new TopCommand(_clientStream, messageId, lineCount))
            {
                response = ExecuteCommand <RetrResponse, TopCommand>(command);
            }

            MimeReader    reader  = new MimeReader(response.MessageLines);
            MimeEntity    entity  = reader.CreateMimeEntity();
            MailMessageEx message = entity.ToMailMessageEx();

            message.Octets        = response.Octets;
            message.MessageNumber = messageId;
            return(entity.ToMailMessageEx());
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     The messages list_ selection changed.
        /// </summary>
        /// <param name="sender">
        ///     The sender.
        /// </param>
        /// <param name="e">
        ///     The e.
        /// </param>
        private void messagesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var setTitle = new Action <string>(
                t =>
            {
                this.Subject.Content = t;
                this.Subject.ToolTip = t;
            });

            // If there are no selected items, then disable the Delete button, clear the boxes, and return
            if (e.AddedItems.Count == 0)
            {
                this.deleteButton.IsEnabled   = false;
                this.forwardButton.IsEnabled  = false;
                this.rawView.Text             = string.Empty;
                this.bodyView.Text            = string.Empty;
                this.htmlViewTab.Visibility   = Visibility.Hidden;
                this.tabControl.SelectedIndex = this.defaultTab.IsVisible ? 0 : 1;

                // Clear fields
                this.FromEdit.Text = string.Empty;
                this.ToEdit.Text   = string.Empty;
                this.CCEdit.Text   = string.Empty;
                this.BccEdit.Text  = string.Empty;
                this.DateEdit.Text = string.Empty;

                var subject = string.Empty;
                this.SubjectEdit.Text = subject;

                this.defaultBodyView.Text = string.Empty;

                this.defaultHtmlView.Content = null;
                this.defaultHtmlView.NavigationService.RemoveBackEntry();
                //this.defaultHtmlView.Refresh();

                setTitle("Papercut");

                return;
            }

            var mailFile = ((MessageEntry)e.AddedItems[0]).File;

            try
            {
                this.tabControl.IsEnabled     = false;
                this.SpinAnimation.Visibility = Visibility.Visible;
                setTitle("Loading...");

                if (this._currentMessageCancellationTokenSource != null)
                {
                    this._currentMessageCancellationTokenSource.Cancel();
                }

                this._currentMessageCancellationTokenSource = new CancellationTokenSource();

                Task.Factory.StartNew(() => { })
                .ContinueWith(
                    task => File.ReadAllLines(mailFile, Encoding.ASCII),
                    this._currentMessageCancellationTokenSource.Token,
                    TaskContinuationOptions.NotOnCanceled,
                    TaskScheduler.Default)
                .ContinueWith(
                    task =>
                {
                    // Load the MIME body
                    var mimeReader = new MimeReader(task.Result);
                    MimeEntity me  = mimeReader.CreateMimeEntity();

                    return(Tuple.Create(task.Result, me.ToMailMessageEx()));
                },
                    this._currentMessageCancellationTokenSource.Token,
                    TaskContinuationOptions.NotOnCanceled,
                    TaskScheduler.Default).ContinueWith(
                    task =>
                {
                    var resultTuple   = task.Result;
                    var mailMessageEx = resultTuple.Item2;

                    // set the raw view...
                    this.rawView.Text = string.Join("\n", resultTuple.Item1);

                    this.bodyView.Text          = mailMessageEx.Body;
                    this.bodyViewTab.Visibility = Visibility.Visible;

                    this.defaultBodyView.Text = mailMessageEx.Body;

                    this.FromEdit.Text = mailMessageEx.From.IfNotNull(s => s.ToString()) ?? string.Empty;
                    this.ToEdit.Text   = mailMessageEx.To.IfNotNull(s => s.ToString()) ?? string.Empty;
                    this.CCEdit.Text   = mailMessageEx.CC.IfNotNull(s => s.ToString()) ?? string.Empty;
                    this.BccEdit.Text  = mailMessageEx.Bcc.IfNotNull(s => s.ToString()) ?? string.Empty;
                    this.DateEdit.Text = mailMessageEx.DeliveryDate.IfNotNull(s => s.ToString()) ?? string.Empty;

                    var subject           = mailMessageEx.Subject.IfNotNull(s => s.ToString()) ?? string.Empty;
                    this.SubjectEdit.Text = subject;

                    setTitle(subject);

                    // If it is HTML, render it to the HTML view
                    if (mailMessageEx.IsBodyHtml)
                    {
                        if (task.IsCanceled)
                        {
                            return;
                        }

                        this.SetBrowserDocument(mailMessageEx);
                        this.htmlViewTab.Visibility = Visibility.Visible;

                        this.defaultHtmlView.Visibility = Visibility.Visible;
                        this.defaultBodyView.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        this.htmlViewTab.Visibility = Visibility.Hidden;
                        if (this.defaultTab.IsVisible)
                        {
                            this.tabControl.SelectedIndex = 0;
                        }
                        else if (Equals(this.tabControl.SelectedItem, this.htmlViewTab))
                        {
                            this.tabControl.SelectedIndex = 2;
                        }

                        this.defaultHtmlView.Visibility = Visibility.Collapsed;
                        this.defaultBodyView.Visibility = Visibility.Visible;
                    }

                    this.SpinAnimation.Visibility = Visibility.Collapsed;
                    this.tabControl.IsEnabled     = true;

                    // Enable the delete and forward button
                    this.deleteButton.IsEnabled  = true;
                    this.forwardButton.IsEnabled = true;
                },
                    this._currentMessageCancellationTokenSource.Token,
                    TaskContinuationOptions.NotOnCanceled,
                    TaskScheduler.FromCurrentSynchronizationContext());
            }
            catch (Exception ex)
            {
                Logger.WriteWarning(string.Format(@"Unable to Load Message ""{0}"": {1}", mailFile, ex));

                setTitle("Papercut");
                this.tabControl.SelectedIndex = 1;
                this.bodyViewTab.Visibility   = Visibility.Hidden;
                this.htmlViewTab.Visibility   = Visibility.Hidden;
            }
        }