Beispiel #1
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));
        }
        public void OpenFile(string fileName)
        {
            if (!System.IO.File.Exists(fileName))
            {
                return;
            }

            view.Clear();
            ShowStatus("Loading...");
            Mouse.OverrideCursor = Cursors.Wait;

            // Load on a background thread so we can keep the UI in sync
            Task.Factory.StartNew(() =>
            {
                try
                {
                    xstFile = new XstFile(view, fileName);
                    xstFile.ReadFolderTree();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error reading xst file");
                }
            })
            // When loading completes, update the UI using the UI thread
            .ContinueWith((task) =>
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    ShowStatus(null);
                    Mouse.OverrideCursor = null;
                    Title = "Xst Reader - " + System.IO.Path.GetFileName(fileName);
                }));
            });
        }
Beispiel #3
0
        public void ExportToFile(string fullFileName, XstFile xstFile)
        {
            if (IsBodyHtml)
            {
                string body = GetBodyAsHtmlString();
                if (MayHaveInlineAttachment)
                {
                    body = EmbedAttachments(body, xstFile);  // Returns null if this is not appropriate
                }
                if (body != null)
                {
                    body = EmbedHtmlPrintHeader(body);
                    using (var stream = new FileStream(fullFileName, FileMode.Create))
                    {
                        var bytes = Encoding.UTF8.GetBytes(body);
                        stream.Write(bytes, 0, bytes.Count());
                    }
                    if (Date != null)
                    {
                        File.SetCreationTime(fullFileName, (DateTime)Date);
                    }
                }
            }
            else if (IsBodyRtf)
            {
#if !NETCOREAPP
                var doc = GetBodyAsFlowDocument();
                EmbedRtfPrintHeader(doc);
                TextRange content = new TextRange(doc.ContentStart, doc.ContentEnd);
                using (var stream = new FileStream(fullFileName, FileMode.Create))
                {
                    content.Save(stream, DataFormats.Rtf);
                }
                if (Date != null)
                {
                    File.SetCreationTime(fullFileName, (DateTime)Date);
                }
#else
                throw new XstException("Emails with body in RTF format not supported on this platform");
#endif
            }
            else
            {
                var body = EmbedTextPrintHeader(Body);
                using (var stream = new FileStream(fullFileName, FileMode.Create))
                {
                    var bytes = Encoding.UTF8.GetBytes(body);
                    stream.Write(bytes, 0, bytes.Count());
                }
                if (Date != null)
                {
                    File.SetCreationTime(fullFileName, (DateTime)Date);
                }
            }
        }
Beispiel #4
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);
        }
Beispiel #5
0
        public void OpenFile(string fileName)
        {
            if (!System.IO.File.Exists(fileName))
            {
                return;
            }

            view.Clear();
            ShowStatus("Loading...");
            Mouse.OverrideCursor = Cursors.Wait;

            // Load on a background thread so we can keep the UI in sync
            Task.Factory.StartNew(() =>
            {
                try
                {
                    xstFile  = new XstFile(fileName);
                    var root = xstFile.ReadFolderTree();

                    //
                    // I refactorized this part:
                    //
                    // iterator with view.RootFolderViews.Add -> moved to view.UpdateFolderViews(root)
                    //

                    // We may be called on a background thread, so we need to dispatch this to the UI thread
                    Application.Current.Dispatcher.Invoke(new Action(() => view.UpdateFolderViews(root)));

                    //foreach (var f in root.Folders)
                    //{
                    //    // We may be called on a background thread, so we need to dispatch this to the UI thread
                    //    Application.Current.Dispatcher.Invoke(new Action(() =>
                    //    {
                    //        view.RootFolderViews.Add(new FolderView(f));
                    //    }));
                    //}
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Error reading xst file");
                }
            })
            // When loading completes, update the UI using the UI thread
            .ContinueWith((task) =>
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    ShowStatus(null);
                    Mouse.OverrideCursor = null;
                    Title = "Xst Reader - " + System.IO.Path.GetFileName(fileName);
                }));
            });
        }
Beispiel #6
0
 public void ExportToFile(string fullFileName, XstFile xstFile)
 {
     if (ShowHtml)
     {
         string body = GetBodyAsHtmlString();
         if (MayHaveInlineAttachment)
         {
             body = EmbedAttachments(body, xstFile);  // Returns null if this is not appropriate
         }
         if (body != null)
         {
             body = EmbedHtmlPrintHeader(body);
             using (var stream = new FileStream(fullFileName, FileMode.Create))
             {
                 var bytes = Encoding.UTF8.GetBytes(body);
                 stream.Write(bytes, 0, bytes.Count());
             }
             if (Date != null)
             {
                 File.SetCreationTime(fullFileName, (DateTime)Date);
             }
         }
     }
     else if (ShowRtf)
     {
         var doc = GetBodyAsFlowDocument();
         EmbedRtfPrintHeader(doc);
         TextRange content = new TextRange(doc.ContentStart, doc.ContentEnd);
         using (var stream = new FileStream(fullFileName, FileMode.Create))
         {
             content.Save(stream, DataFormats.Rtf);
         }
         if (Date != null)
         {
             File.SetCreationTime(fullFileName, (DateTime)Date);
         }
     }
     else
     {
         var body = EmbedTextPrintHeader(Body);
         using (var stream = new FileStream(fullFileName, FileMode.Create))
         {
             var bytes = Encoding.UTF8.GetBytes(body);
             stream.Write(bytes, 0, bytes.Count());
         }
         if (Date != null)
         {
             File.SetCreationTime(fullFileName, (DateTime)Date);
         }
     }
 }
Beispiel #7
0
        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            // Ask for a .ost or .pst file to open
            System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();

            dialog.Filter           = "xst files (*.ost;*.pst)|*.ost;*.pst|All files (*.*)|*.*";
            dialog.FilterIndex      = 1;
            dialog.InitialDirectory = Properties.Settings.Default.LastFolder;
            if (dialog.InitialDirectory == "")
            {
                dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            }
            dialog.RestoreDirectory = true;

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Properties.Settings.Default.LastFolder = System.IO.Path.GetDirectoryName(dialog.FileName);
                Properties.Settings.Default.Save();

                view.Clear();
                txtStatus.Text       = "Loading...";
                Mouse.OverrideCursor = Cursors.Wait;

                // Load on a background thread so we can keep the UI in sync
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        xstFile = new XstFile(view, dialog.FileName);
                        xstFile.ReadFolderTree();
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error reading xst file");
                    }
                })
                // When loading completes, update the UI using the UI thread
                .ContinueWith((task) =>
                {
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        txtStatus.Text       = "";
                        Mouse.OverrideCursor = null;
                        Title = "Xst Reader - " + System.IO.Path.GetFileName(dialog.FileName);
                    }));
                });
            }
        }