简体   繁体   English

从Outlook电子邮件[Drag'n'Drop]获取正文

[英]Get body from Outlook email [Drag’n’Drop]

I'm working with WPF and I'm trying to make a drag'n'drop textbox. 我正在使用WPF,我正在尝试制作一个拖放文本框。
In this textbox I want to get the body of an email which I drag from outlook. 在这个文本框中,我想获取一个我从outlook中拖出的电子邮件的正文。
The code works but I think I need something to “reset” the ActiveExplorer cause now it only shows the last “NEW” email which I drag into the textbox. 代码有效,但我认为我需要一些东西来“重置”ActiveExplorer,因为它现在只显示我拖到文本框中的最后一个“新”电子邮件。

Example: 例:

Drag email 1 -> Textbox - Shows email 1 拖动电子邮件1 - >文本框 - 显示电子邮件1

Drag email 2 -> Textbox - Shows email 2 拖动电子邮件2 - >文本框 - 显示电子邮件2

Drag email 1 -> Textbox - Shows email 2 and email 1 will not be displayed because it already exists in the ActiveExplorer and it will show email 2. 拖动电子邮件1 - >文本框 - 显示电子邮件2和电子邮件1将不会显示,因为它已存在于ActiveExplorer中,它将显示电子邮件2。


Hope my question is a bit clear to you.. 希望我的问题对你来说有点清楚..
Thanks in advance! 提前致谢!

XAML code: XAML代码:

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

XAML code behind: 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();
        }
    }

I moved the declaration of oApp out of DragDrop event as below, and it works as expected. 我将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");
    }
}

--------EDIT-------- - - - - 编辑 - - - -

OR Is it possible that you display only the last item because of this loop? 或者由于此循环,您是否可能仅显示最后一项?

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

I updated LB's answer. 我更新了LB的答案。 His DragEnter EventHandler automatically assumed that the user dropped in something from Outlook. 他的DragEnter EventHandler自动假设用户从Outlook中删除了某些内容。

The result was that if the user dropped in something else (a file, selected text, ...), the code would still look at the currently selected emails in Outlook and ignore what was actually dropped. 结果是,如果用户丢弃其他内容(文件,选定文本,...),代码仍将查看Outlook中当前选定的电子邮件,并忽略实际删除的内容。

The code: 代码:

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

There is a direct cast of the selected Outlook item to Outlook.MailItem . 将所选Outlook项目直接转换为Outlook.MailItem The code thus only works with actual emails. 因此代码仅适用于实际的电子邮件。 It is also possible to handle Outlook.MeetingItem , Outlook.ContactItem , Outlook.NoteItem and probably more. 它也可以处理Outlook.MeetingItemOutlook.ContactItemOutlook.NoteItem以及更多。

Using the version 14.0.0.0 of the Microsoft.Office.Interop.Outlook.dll I cannot use the Outlook.ApplicationClass object. 使用Microsoft.Office.Interop.Outlook.dll的14.0.0.0版本,我无法使用Outlook.ApplicationClass对象。

Instead I used the Outlook.Application in the example you gave and it works like a charm (tested with windows seven & Outlook 2007 SP2). 相反,我在您给出的示例中使用了Outlook.Application ,它就像一个魅力(使用Windows 7和Outlook 2007 SP2测试)。 I can drag&drop emails at will. 我可以随意拖放电子邮件。


PS: MSDN Extract for the ApplicationClass class: PS: ApplicationClass类的MSDN Extract

"This class supports the .NET Framework infrastructure and is not intended to be used directly from your code" “此类支持.NET Framework基础结构,不能直接在您的代码中使用”

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

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