【问题标题】:Get body from Outlook email [Drag’n’Drop]从 Outlook 电子邮件中获取正文 [Drag'and'Drop]
【发布时间】:2011-12-10 02:01:09
【问题描述】:

我正在使用 WPF,并且正在尝试制作一个拖放文本框。
在此文本框中,我想获取从 Outlook 中拖动的电子邮件正文。
该代码有效,但我认为我需要一些东西来“重置”ActiveExplorer,因为现在它只显示我拖入文本框的最后一封“新”电子邮件。

示例:

拖动电子邮件 1 -> 文本框 - 显示电子邮件 1

拖动电子邮件 2 -> 文本框 - 显示电子邮件 2

拖动电子邮件 1 -> 文本框 - 显示电子邮件 2,电子邮件 1 将不会显示,因为它已存在于 ActiveExplorer 中,它将显示电子邮件 2。


希望我的问题对你来说有点清楚..
提前致谢!

XAML 代码:

    <TextBox 
    Name="myTextbox"  
    AllowDrop="True" 
    PreviewDragEnter="email_DragEnter"
    PreviewDrop="email_Drop" />

XAML 代码背后:

    private void email_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

    private void email_Drop(object sender, DragEventArgs e)
    {
        Outlook.ApplicationClass oApp = new Outlook.ApplicationClass();
        Outlook.Explorer oExplorer = oApp.ActiveExplorer();
        Outlook.Selection oSelection = oExplorer.Selection;

        foreach (object item in oSelection)
        {
            Outlook.MailItem mi = (Outlook.MailItem)item;
            myTextbox.Text = mi.Body.ToString();
        }
    }

【问题讨论】:

    标签: c# wpf drag-and-drop outlook


    【解决方案1】:

    我将oApp 的声明移出 DragDrop 事件,如下所示,它按预期工作。

    void Startup()
    {
        _Outlook = new Outlook.Application();
    }
    
    Outlook.Application _Outlook = null;
    
    private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }
    
    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
        richTextBox1.Text = "";
        Outlook.Explorer oExplorer = _Outlook.ActiveExplorer();
        Outlook.Selection oSelection = oExplorer.Selection;
    
        foreach (object item in oSelection)
        {
            Outlook.MailItem mi = (Outlook.MailItem)item;
            richTextBox1.AppendText(mi.Body.ToString() + "\n----------------------------------------\n");
        }
    }
    

    --------编辑--------

    或者是否有可能因为这个循环而只显示最后一项?

    foreach (object item in oSelection)
    {
        Outlook.MailItem mi = (Outlook.MailItem)item;
        myTextbox.Text = mi.Body.ToString(); //<--- Only last items text
    }
    

    【讨论】:

    • 这很好用,但有什么方法可以回复 1 封电子邮件?所以只有你拖的最后一封邮件?
    • 对不起,我不确定我是否理解正确,但如果我拖动一个项目,我只能看到它的文本。如果我选择多个,他们的所有文字都会出现在richtextbox1
    • 并按以下顺序拖动:邮件 1 -> 在该清除文本框之后并拖动一个其他邮件 -> 在该清除文本框之后并再次拖动邮件 1,而不是显示第二个拖动的邮件而不是邮件 1
    • 是的,我也这样做了,我看到的是邮件 1 而不是邮件 2 :(
    • 你能给我发一份简单的测试项目吗?导致它在这里不起作用:(
    【解决方案2】:

    我更新了 L.B 的答案。他的DragEnterEventHandler 自动假定用户从 Outlook 中删除了某些内容。

    结果是,如果用户放入其他内容(文件、选定的文本……),代码仍会查看 Outlook 中当前选择的电子邮件并忽略实际丢弃的内容。

    代码:

    Private _Outlook As Outlook.Application = Nothing
    
    Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        _Outlook = New Outlook.Application()
    End Sub
    
    Private Sub Form_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragEnter
        Dim outlookRequiredFormats = New String() { _
            "RenPrivateSourceFolder", _
            "RenPrivateMessages", _
            "RenPrivateItem", _
            "FileGroupDescriptor", _
            "FileGroupDescriptorW", _
            "FileContents", _
            "Object Descriptor"}
    
        If outlookRequiredFormats.All(Function(requiredFormat) e.Data.GetDataPresent(requiredFormat)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    
    Private Sub Form_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles Me.DragDrop
        Dim oExplorer As Outlook.Explorer = _Outlook.ActiveExplorer()
        Dim oSelection As Outlook.Selection = oExplorer.Selection
        Dim i As Integer = 0
        For Each item As Object In oSelection
            Dim mi As Outlook.MailItem = DirectCast(item, Outlook.MailItem)
            mi.SaveAs("C:\YourPath\message" & i & ".msg")
            i += 1
        Next
    

    将选定的 Outlook 项目直接转换为 Outlook.MailItem。因此,该代码仅适用于实际电子邮件。也可以处理Outlook.MeetingItemOutlook.ContactItemOutlook.NoteItem 甚至更多。

    【讨论】:

      【解决方案3】:

      使用 14.0.0.0 版的 Microsoft.Office.Interop.Outlook.dll 我无法使用 Outlook.ApplicationClass 对象。

      相反,我在您给出的示例中使用了Outlook.Application,它就像一个魅力(使用 Windows 7 和 Outlook 2007 SP2 测试)。我可以随意拖放电子邮件。


      PS:MSDN Extract ApplicationClass 类:

      “此类支持 .NET Framework 基础结构,不打算直接从您的代码中使用”

      【讨论】:

      • 我知道 Outlook.ApplicationClass 嵌入在版本 14.0.0.0 中,但这不是我的问题。我可以拖放邮件,但如果我删除多封邮件,它们会保存在 Outlook.Explorer 中(“就像一种列表”),我只希望显示我拖入文本框的邮件。目前,foreach 会通过 Outlook.Explorer 中的列表并显示最后丢弃的邮件,我只想显示我的活动邮件。
      • 能否请您详细说明您的情况?在最初的示例中,您正在拖动一封电子邮件,而在上一条评论中,您似乎是指拖动多封电子邮件的选择。
      • 我正在拖动signle 电子邮件。我的剧本:[#1]。在文本框中拖动 email_1 = OK ==> 我清除文本框 [#2]。在文本框中拖动 email_2 = OK ==> 我清除文本框 [#3]。在文本框中拖动 email_1 = NOT OK(它显示 email_2)。这是因为 email_2 是 ActiveExplorer 中的最后一封邮件,并且将是我的 foreach 循环中的最后一个 mailItem。
      • 您是否尝试过使用“应用程序”类?
      • 是的,这是我的第一次尝试,但没有成功,所以我尝试了 ApplicationClass,但没有任何改变......
      猜你喜欢
      • 1970-01-01
      • 2011-03-15
      • 2013-10-17
      • 2011-08-24
      • 1970-01-01
      • 2017-10-09
      • 2021-09-30
      • 1970-01-01
      • 2018-10-05
      相关资源
      最近更新 更多