Handle data from .msg file parsed by IndependentSoft’s msg.net package
IndepentSoft’s MSG.NET package is used for parsing of outlook .msg files for reading the contents of file and which can be used to display contents on screen or in our custom viewer.
When the .msg file is parsed by the msg.net package class then the resulting object of msg.net class provides several data values. There can be many properties found in the object that contains Email address information for sender and recipients.
Processing the Recipients Information
The most simpler way to get the recipients information is using the fields –
DisplayCc
DisplayTo
DisplayBcc
These fields contain the Recipients information (in pic below, how this information actually looks when it is opened into the outlook window)
Now according to the above screenshot value of DisplayCc field will be – Richard Montuori; Jai Mistry . These fields contains email addresses in a single string field and not as separate field values.
To fetch more exact information regarding each of recipients whether it is to, cc or bcc, Recipients field property needs to be processed. This field is a list of recipient objects which contains information like DisplayName, RecipientType, AddressType, EmailAddress SmtpAddress etc.
RecipientType field is distinguishing field to detect which type of recipient it is – i.e. Recipient belongs to CC, BCC or TO type email field.
DisplayName field value contains the name of that specific recipient how it appears in the outlook.
Address Type field tells the type of the email address field – i.e. whether email address is of SMTP type or Exchange type(“EX”).
EmailAddress And SmtpAddress fields – These fields contain the value of general format of email addresses for “SMTP” type in AddressType Field.
In some cases, SmtpAddress and email address field remain the same and in some cases smtpaddress field remains null. But email address field always contains the value.
For “EX” type email address email address field displays the Exchange server email addresses like –
/O=FIRST ORGANIZATION/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=users name
Smtpaddress field contains the original email address for “Ex” type email address.
Processing the Sender Information
SenderName contains the value of display name of the Sender similar to DisplayName field of recipients.
SenderAddressType is simliar to Addresstype field for the recipient and contains type of SenderemaiAddress.
SenderEmailAddress is simlar to email address field of recipients.
But in case of Exchange type email addresses, there is no alternative to fetch information directly from any field like for Recipients there is SMTPAddress. But there is a tricky way to fetch the actual sender address information from the TransportMessageHeaders field which is a string type field and contains the header information associated with every email message when sent through SMTP server and which is somewhat analogous to the addressing on an envelope for a letter. Message headers are specific to each email.
This contains some information in the format like-
Received: from BBBPR00MB000.namprd01.prod.outlook.com (..) by BY2PR05MB173.namprd05.prod.outlook.com (..) with Microsoft SMTP Server (TLS) id 15.1.403.16 via Mailbox Transport; Thu, 11 Feb 2016 19:17:16 Authentication-Results: automate.com; dkim=none (message not signed) header.d=none;automate.com; dmarc=none action=none header.from=etfile.com; Content-Type: application/ms-tnef; name="winmail.dat" Content-Transfer-Encoding: binary From: Matt Olbert <molbert@etfile.com> To: "'my-support@webners.com'" <my-support@webners.com> CC: Richard Montuori <myemail1@webners.com>, Jai Mistry <myemail2@webners.com> Subject: Regarding source code Development Thread-Topic: Regarding source code Development Thread-Index: Ajhdjk732160H35B6QAO4BMO//gh4== Date: Thu, 11 Feb 2016 19:17:14 Message-ID: <HJUObgjddkklk501MB1447A827DA145FEsahggg9834D5CDA80@YY1PR1501MB1557.namprd05.prod.outlook.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: yes X-MS-Exchange-Organization-SCL: -1 X-MS-TNEF-Correlator: <HJUObgjddkklk501MB1447A827DA145FEsahggg9834D5CDA80@YY1PR1501MB1557.namprd05.prod.outlook.com> MIME-Version: 1.0 X-MS-Exchange-Organization-MessageDirectionality: Originating X-MS-Exchange-Organization-AuthSource: YY1PR1501MB1557.namprd05.prod.outlook.com X-MS-Exchange-Organization-AuthAs: Internal X-MS-Exchange-Organization-AuthMechanism: 04 X-Originating-IP: [173.76.0.146] X-MS-Exchange-Organization-Network-Message-Id: dddd935f-be97-86ca-if33-08e55517555 Return-Path: molbert@webners.com X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BY1PR0501MB1592; X-MS-Office365-Filtering-Correlation-Id: dddd935f-be97-86ca-if33-08e55517555 X-MS-Exchange-Organization-AVStamp-Service: 1.0 X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: ………………………………………… ………………………………………….
There is Return-Path information in it that contains the actual sender email address information. And we simply use the following C# code to fetch the Value from the Return-Path line.
C# Code:
string transportHeaderInformation=_message.TransportMessageHeaders; string target="Return-Path:"; string[] transportHeaderArray = transportHeaderInformation.Split('\n'); // Split the text using new line character into Array char[] charactersToTrim = { '\r', '\n', ' ' }; var ReturnPath = Array.FindAll(transportHeaderArray, s => s.Contains(target)).FirstOrDefault(); // Find the target text (“Return-Path”) from array if (ReturnPath != null) { ReturnPath = ReturnPath.Substring(ReturnPath.IndexOf(target) + target.Length); // Pick the Text from the location where “Return-Path” ends return ReturnPath.Trim(charactersToTrim); // remove the extra characters from the ReturnPath string value to get the actual sender email address }