简体   繁体   English

如何将Outlook电子邮件拖放到WPF应用程序中

[英]how to drag and save outlook email into WPF application

I am creating a WPF application. 我正在创建一个WPF应用程序。 i want to drag email from outlook into this wpf application and application needs to save it in a specific folder. 我想将电子邮件从Outlook拖到此WPF应用程序中,应用程序需要将其保存在特定的文件夹中。 i have tried using http://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C article. 我尝试使用http://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C文章。 It works fine for winform. 它适用于Winform。 i tried using same code in WPF after fixing all compile time bugs but still it is not working. 修复所有编译时错误后,我尝试在WPF中使用相同的代码,但仍然无法正常工作。

I have searched web a lot but cannot find any working solution for this problem. 我已经在网上搜索了很多,但是找不到针对此问题的任何有效解决方案。 Can someone please provide any working sample code? 有人可以提供任何有效的示例代码吗?

!!! Add references: " Microsoft.Office.Interop.Outlook.dll " !!! 添加引用:“ Microsoft.Office.Interop.Outlook.dll ”! (Search in your disks) (在磁盘中搜索)

Analyzee your DragObject: 分析您的DragObject:

WPF: WPF:

<Window x:Class="WpfApplication1.MainWindow"
        x:Name="thisForm"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <TextBlock TextWrapping="WrapWithOverflow" Drop="ContainerDrop" DragOver="ContainerDragOver" Name="f_DropText" AllowDrop="True"/>
</Window>

c# C#

using System;
using System.IO;
using System.Text;
using System.Windows;
namespace WpfApplication1
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }
    private void ContainerDrop(object sender, DragEventArgs e)
    {
      f_DropText.Text = "";
      StringBuilder sb = new StringBuilder();
      foreach (string format in e.Data.GetFormats())
      {
        sb.AppendLine("Format:" + format);
        try
        {
          object data = e.Data.GetData(format);
          sb.AppendLine("Type:" + (data == null ? "[null]" : data.GetType().ToString()));
          if (format == "FileGroupDescriptor")
          {
            Microsoft.Office.Interop.Outlook.Application OL = new Microsoft.Office.Interop.Outlook.Application();
            sb.AppendLine("##################################################");
            for (int i = 1; i <= OL.ActiveExplorer().Selection.Count; i++)
            {
              Object temp = OL.ActiveExplorer().Selection[i];
              if (temp is Microsoft.Office.Interop.Outlook.MailItem)
              {
                Microsoft.Office.Interop.Outlook.MailItem mailitem = (temp as Microsoft.Office.Interop.Outlook.MailItem);
                int n=1;
                sb.AppendLine("Mail " + i + ": " + mailitem.Subject);
                foreach (Microsoft.Office.Interop.Outlook.Attachment attach in mailitem.Attachments)
                {
                  sb.AppendLine("File " + i + "."+n+": " + attach.FileName);
                  sb.AppendLine("Size " + i + "."+n+": " + attach.Size);
                  sb.AppendLine("For save using attach.SaveAsFile");
                  ++n;
                }
              }
            }
            sb.AppendLine("##################################################");
          }
          else
            sb.AppendLine("Data:" + data.ToString());
        }
        catch (Exception ex)
        {
          sb.AppendLine("!!CRASH!! " + ex.Message);
        }
        sb.AppendLine("=====================================================");
      }
      f_DropText.Text = sb.ToString();
    }
    private void ContainerDragOver(object sender, DragEventArgs e)
    {
      e.Effects = DragDropEffects.Copy;
      e.Handled = true;
    }
  }
}

Finally, I got this working. 终于,我开始工作了。

I took this guys code at http://www.codeproject.com/KB/office/outlook_drag_drop_in_cs.aspx and altered it to work with WPF. 我在http://www.codeproject.com/KB/office/outlook_drag_drop_in_cs.aspx上获取了这些代码,并对其进行了更改以与WPF一起使用。

But it was not easy since there were few constructs that had changed in the underlying COM constructs of System.Windows, so it did not work just by changing from system.windows to swforms.IDataObject. 但是,这并不容易,因为在System.Windows的基础COM构造中几乎没有改变任何构造,因此仅通过从system.windows更改为swforms.IDataObject便无法工作。

Then I found this github code from this thread 然后我从这个线程中找到了这个github代码
https://social.msdn.microsoft.com/Forums/vstudio/en-US/5853bfc1-61ac-4c20-b36c-7ac500e4e2ed/how-to-drag-and-drop-email-msg-from-outlook-to-wpfor-activex-for-upload-and-send-them-out-via?forum=wpf https://social.msdn.microsoft.com/Forums/vstudio/en-US/5853bfc1-61ac-4c20-b36c-7ac500e4e2ed/how-to-drag-and-drop-email-msg-from-outlook-to- wpfor的ActiveX换上传和-发送-他们出过孔?论坛= WPF

http://gist.github.com/521547 - This is the new IDataObject that works with WPF . http://gist.github.com/521547- 这是与WPF兼容的新IDataObject

Has corrections to accomodate System.Windows.IDataObject instead of SWForms.IDataObject and '_innerData' should be used to query for data, along with fixes for some memory access issues. 已进行了修正,以适应System.Windows.IDataObject而不是SWForms.IDataObject和'_innerData'来查询数据,并修复了一些内存访问问题。 The guy has nailed it ! 这家伙已经钉了!

Instead of trying to recover the dropped mail item object you need to detect that something was fropped from Outlook and then connect to the running Outlook instance using the System.Runtime.InteropServices.Marshal.GetActiveObject() method which obtains a running instance of the specified object from the running object table (ROT). 而不是尝试恢复丢失的邮件项目对象,您需要检测是从Outlook中提取了某些东西,然后使用System.Runtime.InteropServices.Marshal.GetActiveObject()方法连接到正在运行的Outlook实例,该方法获取指定对象的运行实例。运行对象表(ROT)中的对象。

Then you can get the Selection object using the Selection property of the Explorer class. 然后,您可以使用Explorer类的Selection属性来获取Selection对象。 It returns a Selection object that contains the item or items that are selected in the explorer window. 它返回一个Selection对象,其中包含在资源管理器窗口中选择的一个或多个项目。

 private void lbAttachments_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetDataPresent("FileGroupDescriptor") ? DragDropEffects.All : DragDropEffects.None;
        }

        private void lbAttachments_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent("FileGroupDescriptor"))
            {
                try {
                    var dataObject = new OutlookDataObject(e.Data);
                    var filePaths = new StringCollection();

                    string[] fileContentNames = (string[])dataObject.GetData("FileGroupDescriptor");
                    if (fileContentNames.Count() > 0)
                    {
                        var fileStreams = (MemoryStream[])dataObject.GetData("FileContents");

                        for (int fileIndex = 0; fileIndex < fileContentNames.Length; fileIndex++)
                        {
                            var ms = fileStreams[fileIndex];
                            var bytes = ms.ToArray();

                            AddAttachment(fileContentNames[fileIndex], bytes);
                        }
                    }
                }
                catch(Exception) { /* ignore */ }

            }
            else if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] s = (string[])e.Data.GetData(DataFormats.FileDrop);
                if (s == null)
                    return;

                foreach (string file in s)
                    AddAttachment(file);
            }
private void AddAttachment(string argFilename)
        {
            var daten = File.ReadAllBytes(argFilename);
            AddAttachment(argFilename, daten);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM