C# 从Outlook电子邮件获取正文[拖放]
我正在与WPF合作,我正在尝试制作一个拖放文本框C# 从Outlook电子邮件获取正文[拖放],c#,wpf,drag-and-drop,outlook,C#,Wpf,Drag And Drop,Outlook,我正在与WPF合作,我正在尝试制作一个拖放文本框 在这个文本框中,我想获取从outlook拖动的电子邮件的正文 代码可以工作,但我想我需要一些东西来“重置”ActiveExplorer,因为现在它只显示我拖到文本框中的最后一封“新”电子邮件 示例: 拖动电子邮件1->文本框-显示电子邮件1 拖动电子邮件2->文本框-显示电子邮件2 拖动email 1->Textbox-显示email 2,email 1将不显示,因为它已存在于ActiveExplorer中,并且将显示email 2 希望我的问
在这个文本框中,我想获取从outlook拖动的电子邮件的正文
代码可以工作,但我想我需要一些东西来“重置”ActiveExplorer,因为现在它只显示我拖到文本框中的最后一封“新”电子邮件 示例: 拖动电子邮件1->文本框-显示电子邮件1 拖动电子邮件2->文本框-显示电子邮件2 拖动email 1->Textbox-显示email 2,email 1将不显示,因为它已存在于ActiveExplorer中,并且将显示email 2
希望我的问题对你来说有点清楚。
提前谢谢 XAML代码:
<TextBox
Name="myTextbox"
AllowDrop="True"
PreviewDragEnter="email_DragEnter"
PreviewDrop="email_Drop" />
使用Microsoft.Office.Interop.Outlook.dll的14.0.0.0版,我无法使用
Outlook.ApplicationClass
对象
相反,在您给出的示例中,我使用了Outlook.Application
,它工作起来就像一个符咒(使用WindowsSeven和Outlook2007SP2进行了测试)。我可以随意拖放电子邮件
注:对于
应用程序类
类:
“此类支持.NET Framework基础结构,不打算直接从代码中使用”
我将
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
}
foreach(oSelection中的对象项)
{
Outlook.MailItem mi=(Outlook.MailItem)项;
myTextbox.Text=mi.Body.ToString();//我更新了L.B的答案。他的DragEnter
EventHandler自动假设用户从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.MeetingItem
、Outlook.ContactItem
、Outlook.NoteItem
,甚至更多。我知道Outlook.ApplicationClass嵌入了14.0.0.0 b版但这不是我的问题。我可以拖放邮件,但如果我拖放多封邮件,它们会保存在Outlook.Explorer中(“类似于列表”)我只想让我拖到文本框中的邮件显示出来。此时,foreach会从Outlook.Explorer中尝试一个列表,并显示最后一封丢弃的邮件,我只想显示我的活动邮件。你能精确描述一下你的场景吗?在最初的示例中,你拖了一封邮件,而在你最后的评论中,你似乎表示拖动多封电子邮件。我正在拖动单个电子邮件。我的屏幕:[1]。在文本框中拖动电子邮件[u 1=OK=>我清除文本框[#2]。在文本框中拖动电子邮件[u 2=OK=>我清除文本框[#3]。在文本框中拖动电子邮件[u 1=NotOK(显示电子邮件[u 2])。这是因为email_2是ActiveExplorer中的最后一封邮件,将是我的foreach循环中的最后一封邮件。您是否尝试使用“应用程序”类?是的,这是我的第一次尝试,但不起作用,因此我尝试使用ApplicationClass,但没有任何更改…这很好,但如何才能返回1封邮件?因此,只有您拖动的最后一封邮件GED?抱歉,我不确定我是否理解正确,但如果我拖动一个项目,我只看到它的文本。如果我选择多个,它们的所有文本都会进入RichTextBox1并按以下顺序拖动:邮件1->在清除文本框之后,拖动另一个邮件->在清除文本框之后,再次拖动邮件1,然后显示第二次拖动的邮件而不是邮件1是的同样,我看到邮件1而不是邮件2:(你能给我发一个简单的测试项目吗?因为它在这里不起作用:(
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