从Outlook电子邮件获取正文
我正在使用WPF,我正在尝试制作一个拖拽文本框。
在这个文本框中,我想获取一个我从outlook中拖出的电子邮件的正文。
该代码有效,但我认为我需要一些东西来“重置”ActiveExplorer,因为它现在只显示我拖到文本框中的最后一个“新”电子邮件。
例:
拖动电子邮件1 – >文本框 – 显示电子邮件1
拖动电子邮件2 – >文本框 – 显示电子邮件2
拖动电子邮件1 – >文本框 – 显示电子邮件2和电子邮件1将不会显示,因为它已存在于ActiveExplorer中,它将显示电子邮件2。
希望我的问题对你来说有点清楚..
提前致谢!
XAML代码:
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(); } }
我将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 }
我更新了LB的答案。 他的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:YourPathmessage" & i & ".msg") i += 1 Next
将所选Outlook项目直接转换为Outlook.MailItem
。 因此,代码仅适用于实际的电子邮件。 它也可以处理Outlook.MeetingItem
, Outlook.ContactItem
, Outlook.NoteItem
以及更多。
使用Microsoft.Office.Interop.Outlook.dll的14.0.0.0版本,我无法使用Outlook.ApplicationClass
对象。
相反,我在您给出的示例中使用了Outlook.Application
,它就像一个魅力(使用Windows 7和Outlook 2007 SP2测试)。 我可以随意拖放电子邮件。
PS: ApplicationClass
类的MSDN Extract :
“此类支持.NET Framework基础结构,不能直接在您的代码中使用”
上述就是C#学习教程:从Outlook电子邮件获取正文分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1039203.html