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

jef*_*smi 9 c# wpf outlook drag-and-drop

我正在使用WPF,我正在尝试制作一个拖放文本框.
在这个文本框中,我想获取一个我从outlook中拖出的电子邮件的正文.
代码有效,但我认为我需要一些东西来"重置"ActiveExplorer,因为它现在只显示我拖到文本框中的最后一个"新"电子邮件.

例:

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

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

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


希望我的问题对你有点清楚..
在此先感谢!

XAML代码:

    <TextBox 
    Name="myTextbox"  
    AllowDrop="True" 
    PreviewDragEnter="email_DragEnter"
    PreviewDrop="email_Drop" />
Run Code Online (Sandbox Code Playgroud)

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

L.B*_*L.B 9

我将oAppDragDrop事件的声明移动到下面,它按预期工作.

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

- - - - 编辑 - - - -

或者由于此循环,您是否可能仅显示最后一项?

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