Exemplo n.º 1
0
        private void SaveAttachments(IEnumerable <Attachment> attachments)
        {
            // Find out where to save the attachments
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();

            dialog.Description  = "Choose folder for saving attachments";
            dialog.RootFolder   = Environment.SpecialFolder.MyComputer;
            dialog.SelectedPath = Properties.Settings.Default.LastAttachmentFolder;
            if (dialog.SelectedPath == "")
            {
                dialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            }
            dialog.ShowNewFolderButton = true;

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Properties.Settings.Default.LastAttachmentFolder = dialog.SelectedPath;
                Properties.Settings.Default.Save();
                try
                {
                    foreach (var a in attachments)
                    {
                        xstFile.SaveAttachment(dialog.SelectedPath, a);
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error saving attachments");
                }
            }
        }
Exemplo n.º 2
0
        public string EmbedAttachments(string body, XstFile xst)
        {
            if (body == null)
            {
                return(null);
            }

            var dict = new Dictionary <string, Attachment>();

            foreach (var a in Attachments.Where(x => x.HasContentId))
            {
                dict.Add(a.ContentId, a);
            }

            return(Regex.Replace(body, @"(="")cid:(.*?)("")", match =>
            {
                Attachment a;

                if (dict.TryGetValue(match.Groups[2].Value, out a))
                {
                    // There are limits to what we can push into an inline data image,
                    // but we don't know exactly what
                    // Todo handle limit when known
                    a.WasRenderedInline = true;
                    var s = new MemoryStream();
                    xst.SaveAttachment(s, a);
                    s.Seek(0, SeekOrigin.Begin);
                    var cooked = match.Groups[1] + @"data:image/jpg;base64," + EscapeString(Convert.ToBase64String(s.ToArray())) + match.Groups[3];
                    return cooked;
                }

                return match.Value;
            }, RegexOptions.Singleline | RegexOptions.IgnoreCase));
        }
Exemplo n.º 3
0
        public void ReadSignedOrEncryptedMessage(XstFile xstFile)
        {
            Attachment a = Message.Attachments[0];

            //get attachment bytes
            var ms = new MemoryStream();

            xstFile.SaveAttachment(ms, a);
            byte[] attachmentBytes = ms.ToArray();

            Message.ReadSignedOrEncryptedMessage(attachmentBytes);
        }
Exemplo n.º 4
0
        private string SaveAttachmentToTemporaryFile(Attachment a)
        {
            if (a == null)
            {
                return(null);
            }

            string fileFullName = Path.ChangeExtension(
                Path.GetTempPath() + Guid.NewGuid().ToString(), Path.GetExtension(a.FileName));;

            try
            {
                xstFile.SaveAttachment(fileFullName, null, listAttachments.SelectedItem as Attachment);
                tempFileNames.Add(fileFullName);
                return(fileFullName);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error saving attachment");
                return(null);
            }
        }
Exemplo n.º 5
0
        private void ShowMessage(Message m)
        {
            try
            {
                //clear any existing status
                ShowStatus(null);

                if (m != null)
                {
                    //email is signed and/or encrypted and no body was included
                    if (m.IsEncryptedOrSigned)
                    {
                        try
                        {
                            Attachment a = m.Attachments[0];

                            //get attachment bytes
                            var ms = new MemoryStream();
                            xstFile.SaveAttachment(ms, a);
                            byte[] attachmentBytes = ms.ToArray();

                            m.ReadSignedOrEncryptedMessage(attachmentBytes);
                        }
                        catch
                        {
                            ShowStatus("Message Failed to Decrypt");
                        }
                    }
                    // Can't bind HTML content, so push it into the control, if the message is HTML
                    if (m.ShowHtml)
                    {
                        string body = m.GetBodyAsHtmlString();
                        if (m.MayHaveInlineAttachment)
                        {
                            body = m.EmbedAttachments(body, xstFile);  // Returns null if this is not appropriate
                        }
                        if (body != null)
                        {
                            // For testing purposes, can show print header in main visualisation
                            if (view.DisplayPrintHeaders)
                            {
                                body = m.EmbedHtmlPrintHeader(body, view.DisplayEmailType);
                            }

                            wbMessage.NavigateToString(body);
                            if (m.MayHaveInlineAttachment)
                            {
                                m.SortAndSaveAttachments();  // Re-sort attachments in case any new in-line rendering discovered
                            }
                        }
                    }
                    // Can't bind RTF content, so push it into the control, if the message is RTF
                    else if (m.ShowRtf)
                    {
                        var body = m.GetBodyAsFlowDocument();

                        // For testing purposes, can show print header in main visualisation
                        if (view.DisplayPrintHeaders)
                        {
                            m.EmbedRtfPrintHeader(body, view.DisplayEmailType);
                        }

                        rtfMessage.Document = body;
                    }
                    // Could bind text content, but use push so that we can optionally add headers
                    else if (m.ShowText)
                    {
                        var body = m.Body;

                        // For testing purposes, can show print header in main visualisation
                        if (view.DisplayPrintHeaders)
                        {
                            body = m.EmbedTextPrintHeader(body, true, view.DisplayEmailType);
                        }

                        txtMessage.Text = body;
                        scrollTextMessage.ScrollToHome();
                    }
                }
                else
                {
                    // Clear the displays, in case we were showing that type before
                    wbMessage.Navigate("about:blank");
                    rtfMessage.Document.Blocks.Clear();
                    txtMessage.Text = "";
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error reading message body");
            }
        }