I have a number of .msg files, which are saved emails from Outlook 2016. Some emails are encrypted, some have attachments. How to figure out if email encrypted? It should work on the server which do not have Office installed. Now I'm writing prove of concept console app, but this code also could be used in web app. Could be subsequent tasks to a get list of recipients if it's encrypted. Is it even possible? It may require decryption, which could be .. difficult, if you are not a recipient. Observation: if I search email string "[email protected]" in the .msg file, then I can find it only if email unencrypted.
My pseudo code below. I installed libary Microsoft.Office.Interop.Outlook by NuGet:
using Microsoft.Office.Interop.Outlook;
namespace EmlReader
{
class Program
{
static void Main(string[] args)
{
const string emailPath = @"C:\Test\temp-saved-mail-enc.msg";
Microsoft.Office.Interop.Outlook.MailItem mailItem = ... load from file // how?
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
}
}
}
PS Related article written by me in relation to Outlook Plugin: How to check if outlook email encrypted? But it related to workstation, not to the server.
CodePudding user response:
A .msg file includes all properties that are present on the Message object that is being stored. You can find the Outlook Item (.msg) File Format described in MSDN.
Be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.
Also you may consider third-party components designed for the server-side execution that doesn't require Office installed on the server.
CodePudding user response:
The message class (PR_MESSAGE_CLASS
) MAPI property will be "IPM.NOTE.SMIME.MultipartSigned"
or "IPM.NOTE.SMIME"
. Keep in mind that OOM always represents signed/encrypted messages as regular (IPM.Note
) messages and MailItem.MessageClass
property will be IPM.Note
.
You can open the MSG file in OutlookSpy (I am its author - click OpenIMsgOnIStg button) to see the PR_MESSAGE_CLASS
MAPI property. You can extract that property using either Extended MAPI (C or Delphi) or one of opensource parsers.