从 Outlook 电子邮件中获取正文 [Drag'n'Drop] [英] Get body from Outlook email [Drag’n’Drop]

查看:29
本文介绍了从 Outlook 电子邮件中获取正文 [Drag'n'Drop]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I’m working with WPF and I’m trying to make a drag’n’drop textbox.
In this textbox I want to get the body of an email which I drag from 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.

示例:

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

Drag email 1 -> Textbox - Shows email 1

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

Drag email 2 -> Textbox - Shows email 2

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


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


Hope my question is a bit clear to you..
Thanks in advance!

XAML 代码:

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

隐藏的 XAML 代码:

XAML code behind:

    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();
        }
    }

推荐答案

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

I moved the declaration of oApp out of DragDrop event as below, and it works as expected.

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() + "
----------------------------------------
");
    }
}

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

--------EDIT--------

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

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
}

这篇关于从 Outlook 电子邮件中获取正文 [Drag'n'Drop]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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