如何将 Outlook 电子邮件拖动并保存到 WPF 应用程序中
how to drag and save outlook email into WPF application
我正在创建 WPF 应用程序。我想将电子邮件从 outlook 拖到这个 wpf 应用程序中,应用程序需要将它保存在一个特定的文件夹中。我试过使用 http://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C 文章。它适用于 winform。修复所有编译时错误后,我尝试在 WPF 中使用相同的代码,但它仍然无法正常工作。
我在网上搜索了很多,但找不到解决此问题的有效方法。有人可以提供任何有效的示例代码吗?
!!!添加引用:“Microsoft.Office.Interop.Outlook.dll”!!!
(在您的磁盘中搜索)
分析你的 DragObject:
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#
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;
}
}
}
您不需要尝试恢复丢失的邮件项目对象,而是需要检测某些东西已从 Outlook 中删除,然后使用 System.Runtime.InteropServices.Marshal.GetActiveObject() 方法连接到 运行 Outlook 实例,该方法获取 运行 指定对象的实例来自 运行 对象 table (ROT)。
然后您可以使用资源管理器class的Selection属性获取Selection对象。它 returns 一个 Selection 对象,其中包含在资源管理器 window 中选择的一个或多个项目。
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);
}
终于,我成功了。
我在 http://www.codeproject.com/KB/office/outlook_drag_drop_in_cs.aspx 获取了这些人的代码并对其进行了修改以与 WPF 一起使用。
但这并不容易,因为在 System.Windows 的基础 COM 结构中几乎没有结构发生变化,所以仅仅从 system.windows 更改为 s.w.forms.IDataObject 是行不通的.
然后我从这个线程
中找到了这个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
http://gist.github.com/521547 - 这是与 WPF 一起使用的新 IDataObject。
已更正以适应 System.Windows.IDataObject 而不是 S.W.Forms.IDataObject,“_innerData”应该用于查询数据,同时修复了一些内存访问问题。这家伙搞定了!
我正在创建 WPF 应用程序。我想将电子邮件从 outlook 拖到这个 wpf 应用程序中,应用程序需要将它保存在一个特定的文件夹中。我试过使用 http://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C 文章。它适用于 winform。修复所有编译时错误后,我尝试在 WPF 中使用相同的代码,但它仍然无法正常工作。
我在网上搜索了很多,但找不到解决此问题的有效方法。有人可以提供任何有效的示例代码吗?
!!!添加引用:“Microsoft.Office.Interop.Outlook.dll”!!! (在您的磁盘中搜索)
分析你的 DragObject:
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#
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;
}
}
}
您不需要尝试恢复丢失的邮件项目对象,而是需要检测某些东西已从 Outlook 中删除,然后使用 System.Runtime.InteropServices.Marshal.GetActiveObject() 方法连接到 运行 Outlook 实例,该方法获取 运行 指定对象的实例来自 运行 对象 table (ROT)。
然后您可以使用资源管理器class的Selection属性获取Selection对象。它 returns 一个 Selection 对象,其中包含在资源管理器 window 中选择的一个或多个项目。
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);
}
终于,我成功了。
我在 http://www.codeproject.com/KB/office/outlook_drag_drop_in_cs.aspx 获取了这些人的代码并对其进行了修改以与 WPF 一起使用。
但这并不容易,因为在 System.Windows 的基础 COM 结构中几乎没有结构发生变化,所以仅仅从 system.windows 更改为 s.w.forms.IDataObject 是行不通的.
然后我从这个线程
中找到了这个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
http://gist.github.com/521547 - 这是与 WPF 一起使用的新 IDataObject。
已更正以适应 System.Windows.IDataObject 而不是 S.W.Forms.IDataObject,“_innerData”应该用于查询数据,同时修复了一些内存访问问题。这家伙搞定了!