MsgReader 5.7.0
dotnet add package MsgReader --version 5.7.0
NuGet\Install-Package MsgReader -Version 5.7.0
<PackageReference Include="MsgReader" Version="5.7.0" />
paket add MsgReader --version 5.7.0
#r "nuget: MsgReader, 5.7.0"
// Install MsgReader as a Cake Addin #addin nuget:?package=MsgReader&version=5.7.0 // Install MsgReader as a Cake Tool #tool nuget:?package=MsgReader&version=5.7.0
What is MSGReader
MSGReader is a C# .NET 4.6.2, .NET Standard 2.0 and .NET Standard 2.1 assembly to read Outlook MSG and EML (Mime 1.0) files. Almost all common object in Outlook are supported:
- Appointment
- Task
- Contact card
- Sticky note
It supports all body types there are in MSG files, this includes:
- Text
- HTML
- HTML embedded into RTF
- RTF
MSGReader has only a few options to manipulate an MSG file. The only option you have is that you can remove attachments and then save the file to a new one.
If you realy want to write MSG files then see my MsgKit project on GitHub (https://github.com/Sicos1977/MsgKit)
Read properties from an Outlook (msg) message
using (var msg = new MsgReader.Outlook.Storage.Message("d:\\testfile.msg"))
{
var from = msg.Sender;
var sentOn = msg.SentOn;
var recipientsTo = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.To, false, false);
var recipientsCc = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.Cc, false, false);
var subject = msg.Subject;
var htmlBody = msg.BodyHtml;
// etc...
}
Detecting charset encoding in MSG files with HTML encapuslated into RTF that use different font set encodings
Most of the times when an HTML body is used in an MSG file this HTML body is encapsulated into RTF. See this link for more info --> https://learn.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxrtfex/4f09a809-9910-43f3-a67c-3506b09ca5ac
When an HTML body contains chars that are not in the default extended ASCII range then these chars are encoded. This is normally not a problem when just one language is used. When multiple languages are used then it is quite often that the RTF is not build correctly in a way so that MSGReader can figure out what kind of encoding needs to be used to decode the chars. Because of this MSGReader uses the nuget package UTF.Unknown (https://www.nuget.org/packages/UTF.Unknown/) to try to figure out in what kind of encoding a char is stored. Most of the times this works correctly and because of that a threshold is set to a value of 0.90 so that when the detection level passes this value it will be seen as a valid char.
If you still have bad results you can control this confidence level yourself by using the property CharsetDetectionEncodingConfidenceLevel
in the Reader
or Message
class
/// <summary>
/// When an MSG file contains an RTF file with encapsulated HTML and the RTF
/// uses fonts with different encodings then this levels set the threshold that
/// an encoded string detection levels needs to be before recognizing it as a valid
/// string. When the detection level is lower than this setting then the default RTF
/// encoding is used to decode the encoded char
/// </summary>
/// <remarks>
/// Default this value is set to 0.90, any values lower then 0.70 probably give bad
/// results
/// </remarks>
public float CharsetDetectionEncodingConfidenceLevel { get; set; } = 0.90f;
Read properties from an Outlook (eml) message
var fileInfo = new FileInfo("d:\\testfile.eml");
var eml = MsgReader.Mime.Message.Load(fileInfo);
if (eml.Headers != null)
{
if (eml.Headers.To != null)
{
foreach (var recipient in eml.Headers.To)
{
var to = recipient.Address;
}
}
}
var subject = eml.Headers.Subject;
if (eml.TextBody != null)
{
var textBody = System.Text.Encoding.UTF8.GetString(eml.TextBody.Body);
}
if (eml.HtmlBody != null)
{
var htmlBody = System.Text.Encoding.UTF8.GetString(eml.HtmlBody.Body);
}
// etc...
Delete attachment from an Outlook message
This example deletes the first attachment
var outlook = new Storage.Message(fileName, FileAccess.ReadWrite);
outlook.DeleteAttachment(outlook.Attachments[0]);
outlook.Save("d:\\deleted.msg");
Translations
Kees van Spelde
- English (US)
- Dutch
Ronald Kohl
- German
Yan Grenier (@ygrenier on GitHub)
- French
xupefei
- Simpl Chinese
Installing via NuGet
The easiest way to install MSGReader is via NuGet.
In Visual Studio's Package Manager Console, simply enter the following command:
Install-Package MSGReader
Side note
This project can also be used from a COM based language like VB script or VB6. To use it first compile the code and register the com visible assembly with the command:
Regasm.exe /codebase MsgReader.dll
After that you can call it like this:
dim msgreader
set msgreader = createobject("MsgReader.Reader")
msgreader.ExtractToFolderFromCom "the msg file to read", "the folder where to place the extracted files"
License Information
MsgReader is Copyright (C) 2013-2024 Magic-Sessions and is licensed under the MIT license:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Core Team
Sicos1977 (Kees van Spelde)
Support
If you like my work then please consider a donation as a thank you by using the donate button at the top
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.1)
- Microsoft.Maui.Graphics (>= 8.0.92)
- Newtonsoft.Json (>= 13.0.3)
- OpenMcdf (>= 2.3.1)
- RtfPipe (>= 2.0.7677.4303)
- System.Drawing.Common (>= 8.0.10)
- System.Security.Cryptography.Pkcs (>= 8.0.1)
- UTF.Unknown (>= 2.5.1)
-
.NETStandard 2.0
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.1)
- Microsoft.Maui.Graphics (>= 8.0.92)
- Newtonsoft.Json (>= 13.0.3)
- OpenMcdf (>= 2.3.1)
- RtfPipe (>= 2.0.7677.4303)
- System.Security.Cryptography.Pkcs (>= 8.0.1)
- UTF.Unknown (>= 2.5.1)
-
.NETStandard 2.1
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.1)
- Microsoft.Maui.Graphics (>= 8.0.92)
- Newtonsoft.Json (>= 13.0.3)
- OpenMcdf (>= 2.3.1)
- RtfPipe (>= 2.0.7677.4303)
- System.Security.Cryptography.Pkcs (>= 8.0.1)
- UTF.Unknown (>= 2.5.1)
NuGet packages (9)
Showing the top 5 NuGet packages that depend on MsgReader:
Package | Downloads |
---|---|
SuperOffice.NetServer.Core
SuperOffice NetServer Core is SuperOffice's data access technology for new applications. This is for onsite installations with the same version. This also works with SuperOffice Online. |
|
Toxy
.NET text extraction framework |
|
invoisys.SDK.EML
Package Description |
|
PasswordProtectedChecker
Package Description |
|
OfficeExtractor
Extracts embedded OLE objects from Word, Excel, PowerPoint and Open Office files without needing the original programs. |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on MsgReader:
Repository | Stars |
---|---|
QL-Win/QuickLook
Bring macOS “Quick Look” feature to Windows
|
- Fix issue with Hebrew encoding
- Fix issue with signed em parts
- Other fixes