Create / Open existing msg from path to new Outlook.MailItem in C #

Hello, I would like to create Outlook.MailItem (I believe) from an existing one on disk. I have a path stored in a string and I would like to access it and save it.

I can't figure out how to open it in C # and access it.

I currently have something like

where fl evaluates 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; 
+2
source share
2 answers

To open an item in Outlook, follow these steps:

var email = (Outlook.MailItem)app.Session.OpenSharedItem(fl)

From there, you can access the Attachments property and the Body property.

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

+7
source

I used this NuGet package: https://www.nuget.org/packages/MSGReader/

Everything seems to be fine. I prefer it in the MS OutlookApi library because it does not require Outlook installation.

I appreciate that it will not create MailItem instances, as you requested in your question, but it will allow you to retrieve the stored individual attachments and body ...

0
source

Source: https://habr.com/ru/post/989329/


All Articles