Пример #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());
        }
Пример #2
0
        private static MailMessageEx GetMailMessage(string file)
        {
            var allLines   = File.ReadAllLines(file);
            var mimeReader = new MimeReader(allLines);

            return(mimeReader.CreateMimeEntity().ToMailMessageEx());
        }
Пример #3
0
        private static void DATA(Connection connection)
        {
            // Check command order
            if (connection.Session.Sender == null || connection.Session.MailFrom == null || connection.Session.Recipients.Count == 0)
            {
                connection.Send("503 Bad sequence of commands");
                return;
            }

            string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyyMMddHHmmssFF") + ".eml");

            try
            {
                var lines = new StringBuilder();

                using (var networkStream = new NetworkStream(connection.Client, false))
                    using (var reader = new StreamReader(networkStream))
                    {
                        connection.Send("354 Start mail input; end with <CRLF>.<CRLF>").AsyncWaitHandle.WaitOne();
                        string line;

                        while ((line = reader.ReadLine()) != ".")
                        {
                            lines.AppendLine(line);
                        }
                    }

                var mimeReader = new MimeReader(lines.ToString().Replace("\r\n", "\n").Split('\n'));
                var entity     = mimeReader.CreateMimeEntity();
                var message    = entity.ToMailMessageEx();
                OnMessageReceived(connection, message);
            }
            catch (IOException e)
            {
                Logger.WriteWarning("IOException received in Processor.DATA while reading message.  Closing connection.  Message: " + e.Message, connection.ConnectionId);
                connection.Close();
                return;
            }

            connection.Send("250 OK");
        }
Пример #4
0
        /// <summary>
        /// Retrs the specified message.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns>A MimeEntity for the requested Pop3 Mail Item.</returns>
        public MimeEntity RetrMimeEntity(Pop3ListItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            if (item.MessageId < 1)
            {
                throw new ArgumentOutOfRangeException("item.MessageId");
            }

            RetrResponse response;
            using (RetrCommand command = new RetrCommand(_clientStream, item.MessageId))
            {
                response = ExecuteCommand<RetrResponse, RetrCommand>(command);
            }

            MimeReader reader = new MimeReader(response.MessageLines);
            return reader.CreateMimeEntity();
        }
Пример #5
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;
            }
        }