c# - 如何捕获电子邮件

标签 c# sharepoint outlook-addin outlook-2010

我在 Outlook 中创建了一个基本的自定义任务 Pane 。

我想将电子邮件拖放到任务 Pane 中。放下后,它应该允许我将电子邮件捕获为我猜的对象,允许我用它做一些事情,例如保存到共享点位置。

这可能吗?如果是这样,有什么指示吗?

我正在使用 VS2013 C# .NET 4.0,加载项适用于 Outlook 2010/2013。

最佳答案

先决条件和设置

  • Windows 10 专业版
  • Visual Studio 2013 Ultimate 与 Office 开发
  • 带有电子邮件帐户的 Outlook 2013

项目

  • 在 Visual Studio 中选择新建项目 -> Visual C# -> Office/SharePoint -> Office 加载项 -> Outlook 2013 加载项
  • 右键单击项目 -> 添加 -> 用户控件
  • 打开“ThisAddIn.cs”并将以下代码添加到“ThisAddIn_Startup”方法中:

    var myCustomPane= this.CustomTaskPanes.Add(new UserControl1(), "My Pane");
    myCustomPane.Visible = true;
    

Outlook 2013 Custom Pane

拖放消息

  • 在解决方案资源管理器中双击 UserControl1。这将打开设计器窗口。
  • 在属性中设置 AllowDrop = True 并连接两个事件处理程序 DragDropDragEnter

    private void UserControl1_DragEnter(object sender, DragEventArgs e)
    {
        // if you want to read the message data as a string use this:
        if (e.Data.GetDataPresent(DataFormats.UnicodeText))
        {
            e.Effect = DragDropEffects.Copy;
        }
        // if you want to read the whole .msg file use this:
        if (e.Data.GetDataPresent("FileGroupDescriptorW") && 
            e.Data.GetDataPresent("FileContents"))
        {
            e.Effect = DragDropEffects.Copy;
        }
    }
    
    private void UserControl1_DragDrop(object sender, DragEventArgs e)
    {
        // to read basic info about the mail use this:
        var text = e.Data.GetData(DataFormats.UnicodeText).ToString();
        var message = text.Split(new string[] { "\r\n" }, StringSplitOptions.None)[1];
        var parts = message.Split('\t');
        var from = parts[0]; // Email From
        var subject = parts[1]; // Email Subject
        var time = parts[2]; // Email Time
    
        // to get the .msg file contents use this:
        // credits to "George Vovos", http://stackoverflow.com/a/43577490/1093508
        var outlookFile = e.Data.GetData("FileGroupDescriptor", true) as MemoryStream;
        if (outlookFile != null)
        {
            var dataObject = new iwantedue.Windows.Forms.OutlookDataObject(e.Data);
    
            var filenames = (string[])dataObject.GetData("FileGroupDescriptorW");
            var filestreams = (MemoryStream[])dataObject.GetData("FileContents");
    
            for (int fileIndex = 0; fileIndex < filenames.Length; fileIndex++)
            {
                string filename = filenames[fileIndex];
                MemoryStream filestream = filestreams[fileIndex];
    
                // do whatever you want with filestream, e.g. save to a file:
                string path = Path.GetTempPath() + filename;
                using (var outputStream = File.Create(path))
                {
                    filestream.WriteTo(outputStream);
                }
            }
        }
    }
    

您可以从CodeProject 获取"iwantedue.Windows.Forms.OutlookDataObject"或者你可以使用这个 GitHub gist .

演示

Outlook 2013 Custom Pane Drag and drop email message

关于c# - 如何捕获电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43544425/

相关文章:

c# - 尝试连接到 Windows Azure 服务总线时出现 DNS 错误

c# - NAudio 音量改变

c# - REST 接口(interface)参数规范中的术语不明确?

javascript - 自定义过滤器,用于替换从 Sharepoint 返回的 json 日期中的某些字符串字符

javascript - 如何使用 Javascript 从加载项中的 Outlook 获取默认选择的格式

c# - 如何在 WCF 中返回 IEnumerable ?现有连接被远程主机强制关闭

c# - 列 Oracle 到 MySQL 的值超出范围

multithreading - 如何在 Outlook 2007 加载项中高效运行后台任务?

azure - 获得未经授权的访问以将数据从 blob 移动到共享点

outlook - 从存储在 Exchange 中的 ContactInfo 获取 Smtp 电子邮件