在 C# 中创建/打开从路径到新 Outlook.MailItem 的现有消息

发布于 2024-11-26 10:10:36 字数 912 浏览 0 评论 0原文

您好,我想从磁盘上的现有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缪败 2024-12-03 10:10:36

要在 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 and Body property as well.

Also, as I mentioned in my comment if the Regex.IsMatch is to determing the file extension, use Path.GetExtension() instead

坦然微笑 2024-12-03 10:10:36

我使用了这个 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文