Esempio n. 1
0
        /// <summary>
        /// Example showing:
        ///  - how to a find plain text version in a Message
        ///  - how to save MessageParts to file
        /// </summary>
        /// <param name="message">The message to examine for plain text</param>
        public static void FindPlainTextInMessage(OPMessage message)
        {
            MessagePart plainText = message.FindFirstPlainTextVersion();

            if (plainText != null)
            {
                // Save the plain text to a file, database or anything you like
                plainText.Save(new FileInfo("plainText.txt"));
            }
        }
Esempio n. 2
0
        private void ListMessagesMessageSelected(object sender, TreeViewEventArgs e)
        {
            // Fetch out the selected message
            OPMessage message = messages[GetMessageNumberFromSelectedNode(listMessages.SelectedNode)];

            // If the selected node contains a MessagePart and we can display the contents - display them
            if (listMessages.SelectedNode.Tag is MessagePart)
            {
                MessagePart selectedMessagePart = (MessagePart)listMessages.SelectedNode.Tag;
                if (selectedMessagePart.IsText)
                {
                    // We can show text MessageParts
                    messageTextBox.Text = selectedMessagePart.GetBodyAsText();
                }
                else
                {
                    // We are not able to show non-text MessageParts (MultiPart messages, images, pdf's ...)
                    messageTextBox.Text = "<<OpenPop>> Cannot show this part of the email. It is not text <<OpenPop>>";
                }
            }
            else
            {
                // If the selected node is not a subnode and therefore does not
                // have a MessagePart in it's Tag property, we genericly find some content to show

                // Find the first text/plain version
                MessagePart plainTextPart = message.FindFirstPlainTextVersion();
                if (plainTextPart != null)
                {
                    // The message had a text/plain version - show that one
                    messageTextBox.Text = plainTextPart.GetBodyAsText();
                }
                else
                {
                    // Try to find a body to show in some of the other text versions
                    List <MessagePart> textVersions = message.FindAllTextVersions();
                    if (textVersions.Count >= 1)
                    {
                        messageTextBox.Text = textVersions[0].GetBodyAsText();
                    }
                    else
                    {
                        messageTextBox.Text = "<<OpenPop>> Cannot find a text version body in this message to show <<OpenPop>>";
                    }
                }
            }

            // Clear the attachment list from any previus shown attachments
            listAttachments.Nodes.Clear();

            // Build up the attachment list
            List <MessagePart> attachments = message.FindAllAttachments();

            foreach (MessagePart attachment in attachments)
            {
                // Add the attachment to the list of attachments
                TreeNode addedNode = listAttachments.Nodes.Add((attachment.FileName));

                // Keep a reference to the attachment in the Tag property
                addedNode.Tag = attachment;
            }

            // Only show that attachmentPanel if there is attachments in the message
            bool hadAttachments = attachments.Count > 0;

            attachmentPanel.Visible = hadAttachments;

            // Generate header table
            DataSet   dataSet = new DataSet();
            DataTable table   = dataSet.Tables.Add("Headers");

            table.Columns.Add("Header");
            table.Columns.Add("Value");

            DataRowCollection rows = table.Rows;

            // Add all known headers
            rows.Add(new object[] { "Content-Description", message.Headers.ContentDescription });
            rows.Add(new object[] { "Content-Id", message.Headers.ContentId });
            foreach (string keyword in message.Headers.Keywords)
            {
                rows.Add(new object[] { "Keyword", keyword });
            }
            foreach (RfcMailAddress dispositionNotificationTo in message.Headers.DispositionNotificationTo)
            {
                rows.Add(new object[] { "Disposition-Notification-To", dispositionNotificationTo });
            }
            foreach (Received received in message.Headers.Received)
            {
                rows.Add(new object[] { "Received", received.Raw });
            }
            rows.Add(new object[] { "Importance", message.Headers.Importance });
            rows.Add(new object[] { "Content-Transfer-Encoding", message.Headers.ContentTransferEncoding });
            foreach (RfcMailAddress cc in message.Headers.Cc)
            {
                rows.Add(new object[] { "Cc", cc });
            }
            foreach (RfcMailAddress bcc in message.Headers.Bcc)
            {
                rows.Add(new object[] { "Bcc", bcc });
            }
            foreach (RfcMailAddress to in message.Headers.To)
            {
                rows.Add(new object[] { "To", to });
            }
            rows.Add(new object[] { "From", message.Headers.From });
            rows.Add(new object[] { "Reply-To", message.Headers.ReplyTo });
            foreach (string inReplyTo in message.Headers.InReplyTo)
            {
                rows.Add(new object[] { "In-Reply-To", inReplyTo });
            }
            foreach (string reference in message.Headers.References)
            {
                rows.Add(new object[] { "References", reference });
            }
            rows.Add(new object[] { "Sender", message.Headers.Sender });
            rows.Add(new object[] { "Content-Type", message.Headers.ContentType });
            rows.Add(new object[] { "Content-Disposition", message.Headers.ContentDisposition });
            rows.Add(new object[] { "Date", message.Headers.Date });
            rows.Add(new object[] { "Date", message.Headers.DateSent });
            rows.Add(new object[] { "Message-Id", message.Headers.MessageId });
            rows.Add(new object[] { "Mime-Version", message.Headers.MimeVersion });
            rows.Add(new object[] { "Return-Path", message.Headers.ReturnPath });
            rows.Add(new object[] { "Subject", message.Headers.Subject });

            // Add all unknown headers
            foreach (string key in message.Headers.UnknownHeaders)
            {
                string[] values = message.Headers.UnknownHeaders.GetValues(key);
                if (values != null)
                {
                    foreach (string value in values)
                    {
                        rows.Add(new object[] { key, value });
                    }
                }
            }

            // Now set the headers displayed on the GUI to the header table we just generated
            gridHeaders.DataMember = table.TableName;
            gridHeaders.DataSource = dataSet;
        }