如何将Outlook电子邮件拖放到WPF应用程序中 [英] how to drag and save outlook email into WPF application

查看:108
本文介绍了如何将Outlook电子邮件拖放到WPF应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建WPF应用程序.我想将电子邮件从Outlook拖到此WPF应用程序中,而应用程序需要将其保存在特定的文件夹中.我尝试使用 http://www.codeproject.com /Articles/28209/Outlook-Drag-and-Drop-in-C 文章.它适用于Winform.修复所有编译时错误后,我尝试在WPF中使用相同的代码,但仍然无法正常工作.

I am creating a WPF application. i want to drag email from outlook into this wpf application and application needs to save it in a specific folder. i have tried using http://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C article. It works fine for winform. i tried using same code in WPF after fixing all compile time bugs but still it is not working.

我已经在网上进行了大量搜索,但是找不到针对此问题的任何有效解决方案.有人可以提供任何有效的示例代码吗?

I have searched web a lot but cannot find any working solution for this problem. Can someone please provide any working sample code?

推荐答案

!!!添加引用:" Microsoft.Office.Interop.Outlook.dll "! (在磁盘中搜索)

!!! Add references: "Microsoft.Office.Interop.Outlook.dll" !!! (Search in your disks)

分析您的DragObject:

Analyzee your 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电子邮件拖放到WPF应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆