[英]How to open .eml files using Outlook MAPI in C#?
我有一个 C# 应用程序,它读取 .msg 文件并提取正文和附件。 但是当我尝试加载 a.eml 文件时,应用程序崩溃了。 我正在加载这样的文件:
MailItem mailItem = (MailItem)outlookApp.CreateItemFromTemplate(msgFileName);
mailItem.SaveAs(fullFilename, OlSaveAsType.olHTML); // save body in html format
for(int i = 0; i < mailItem.Attachments.Count; i++)
mailItem.Attachments[i].SaveAsFile(filename); // save attachments
这适用于 .msg 文件,但不适用于 .eml 文件。 我不明白为什么 .eml 文件不起作用,因为我可以在 Outlook 2010 中打开 .eml 文件。
如何使用 Outlook主互操作程序集加载 .eml 文件?
试试这个示例代码轻松从.EML文件中检索电子邮件信息
CreateItemFromTemplate
仅适用于MSG / OFT文件。 在EML文件中,您需要在代码中显式解析文件或使用第三方库(例如Redemption):
以下代码将创建一个MSG文件,并使用Redemption ( RDOSession对象)将EML文件导入其中:
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = outlookApp.Session.MAPIOBJECT
set Msg = Session.CreateMessageFromMsgFile("C:\Temp\temp.msg")
Msg.Import "C:\Temp\test.eml", 1024
Msg.Save
MsgBox Msg.Subject
然后,您可以使用消息( RDOMail )访问它的各种属性(主题,正文等)
要从.eml文件创建MailItem,您可以使用以下两个步骤:首先打开Outlook流程实例,然后使用Outlook API创建MailItem。
string file = @"C:\TestEML\EmlMail.eml";
System.Diagnostics.Process.Start(file);
Outlook.Application POfficeApp = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application"); // note that it returns an exception if Outlook is not running
Outlook.MailItem POfficeItem = (Outlook.MailItem)POfficeApp.ActiveInspector().CurrentItem; // now pOfficeItem is the COM object that represents your .eml file
虽然Outlook可以打开EML文件 ,但是无法仅使用VBA 以编程方式执行此操作 。 所以我创建了这个VBA宏,它循环遍历某个文件夹并使用SHELL EXEC打开每个EML文件。 在Outlook打开EML文件之前可能需要几毫秒,因此VBA会等待,直到ActiveInspector中的某些内容打开。 最后,将此电子邮件复制到某个选定的文件夹中,并且(如果成功)将删除原始EML文件。
请在此处查看我的完整答案(和代码): https : //stackoverflow.com/a/33761441/3606250
假设安装了 outlook ...
只有一个 msg 文件,当 outlook 正在运行/已经打开时,使用 OpenSharedItem
Microsoft.Office.Interop.Outlook.Application appOutlook = new Microsoft.Office.Interop.Outlook.Application();
var myMailItem = appOutlook.Session.OpenSharedItem(strPathToSavedEmailFile) as Microsoft.Office.Interop.Outlook.MailItem;
对于 eml 文件或 msg 文件(Outlook 将打开并弹出):
var strPathToSavedEmailFile=@"C:\temp\mail.eml";
//Microsoft.Win32 namespace to get path from registry
var strPathToOutlook=Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\App Paths\outlook.exe").GetValue("").ToString();
//var strPathToOutlook=@"C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE";
Microsoft.Office.Interop.Outlook.Application appOutlook = new Microsoft.Office.Interop.Outlook.Application();
//Microsoft.Office.Interop.Outlook.MailItem myMailItem = (Microsoft.Office.Interop.Outlook.MailItem)appOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
string strOutlookArgs;
if(Path.GetExtension(strPathToSavedEmailFile)==".eml")
{
strOutlookArgs = @"/eml "+strPathToSavedEmailFile; // eml is an undocumented outlook switch to open .eml files
}else
{
strOutlookArgs = @"/f "+strPathToSavedEmailFile;
}
Process p = new System.Diagnostics.Process();
p.StartInfo = new ProcessStartInfo()
{
CreateNoWindow = false,
FileName = strPathToOutlook,
Arguments = strOutlookArgs
};
p.Start();
Task.Delay(TimeSpan.FromSeconds(5)).GetAwaiter().GetResult();
var myMailItem = (Microsoft.Office.Interop.Outlook.MailItem)appOutlook.ActiveInspector().CurrentItem;
Console.WriteLine(myMailItem.Subject);
foreach(Microsoft.Office.Interop.Outlook.Attachment mailAttachment in myMailItem.Attachments){
Console.WriteLine(mailAttachment.FileName);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.