在 C# 中创建/打开从路径到新 Outlook.MailItem 的现有消息
您好,我想从磁盘上的现有 Outlook.MailItem 创建一个 Outlook.MailItem (我相信)。我将路径存储在字符串中,并且希望访问以保存其中的正文和附件。
我似乎不知道如何在 C# 中打开它并访问它。
的内容,
目前,我有一些类似于 fl 评估为“C:\users\msgs\email.msg”
谢谢您的时间
Outlook.Application app = new Outlook.Application();
try
{
foreach (String fl in Directory.GetFiles(docInfo.LocalPath + _preprocessorDirectory))
{
if (Regex.IsMatch(fl.Trim(), _regex, RegexOptions.IgnoreCase))
{
Outlook.MailItem email = new Outlook.MailItem(fl);
SaveAttachments(email);
SaveBody(email);
}
}
}
catch (Exception ex)
{
logger.Error("Error in Process for document " + docInfo.OriginalPath, ex);
callback.Invoke(docInfo, false);
}
return false;
Hello I'd like to create a Outlook.MailItem ( I believe ) from an existing one located on disk. I have the path stored in a string, and would like to access to save the body and attachments from it.
I can't seem to figure out how to open it in c# and access it.
currently I have something along the lines of
where fl evaluates out to something like "C:\users\msgs\email.msg"
Thanks for the time
Outlook.Application app = new Outlook.Application();
try
{
foreach (String fl in Directory.GetFiles(docInfo.LocalPath + _preprocessorDirectory))
{
if (Regex.IsMatch(fl.Trim(), _regex, RegexOptions.IgnoreCase))
{
Outlook.MailItem email = new Outlook.MailItem(fl);
SaveAttachments(email);
SaveBody(email);
}
}
}
catch (Exception ex)
{
logger.Error("Error in Process for document " + docInfo.OriginalPath, ex);
callback.Invoke(docInfo, false);
}
return false;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要在 Outlook 中打开项目,请尝试:
var email = (Outlook.MailItem)app.Session.OpenSharedItem(fl)
从那里,您可以访问
Attachments
属性和Body
属性也是如此。另外,正如我在评论中提到的,如果
Regex.IsMatch
要确定文件扩展名,请使用 Path.GetExtension() 代替To open an item in outlook try:
var email = (Outlook.MailItem)app.Session.OpenSharedItem(fl)
From there, you can access the
Attachments
property andBody
property as well.Also, as I mentioned in my comment if the
Regex.IsMatch
is to determing the file extension, use Path.GetExtension() instead我使用了这个 NuGet 包: https://www.nuget.org/packages/MSGReader/
似乎工作正常。与 MS OutlookApi 库相比,我更喜欢它,因为它不需要安装 Outlook。
我很高兴它不会创建 MailItem 的实例,正如您在问题中所要求的那样 - 但它将使您能够提取保存单个附件和正文...
I used this NuGet package: https://www.nuget.org/packages/MSGReader/
Seems to work fine. I prefer it to the MS OutlookApi library because it doesn't require Outlook to be installed.
I appreciate that it won't create instances of MailItem, as you have asked for in your question - but it will enable you to extract save the individual attachments and the body...