'Reading an .eml (outlook email) file using c#
I'm writing a service that cleans files by removing all malicious content. I'm using Interop Excel & Word api's like this:
Excel
var excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.Visible = false;
excelApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;
try
{
var workbook = excelApp.Workbooks.Open(fileToClean.InputFileName);
Word
var wordApp = new Microsoft.Office.Interop.Word.Application
{
Visible = false,
AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable
};
try
{
var wordDoc = wordApp.Documents.Open(fileToClean.InputFileName, false, true);
I'm trying to find a similar way to open .eml Outlook files. I can't find any way of opening a .eml file using the Outlook Interop.
Solution 1:[1]
EML file format is not native for Outlook - MSG format is (you can open an MSG file using Namespace.GetSharedItem
). Even if Outlook will be happy to open an EML file for you if you double click on it in Windows Explorer.
To read an EML file, you would need to either parse it explicitly in your code, or to use one of numerous MIME processing libraries. If using Redemption (I am its author) is an option, you can create a temporary MSG file and import EML file into it.
The following code will create an MSG file and import an EML file into it using Redemption (RDOSession object):
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = outlookApp.Session.MAPIOBJECT
set Msg = Session.CreateMessageFromMsgFile("C:\Temp\temp.msg")
Msg.Import "C:\Temp\test.eml", 1024
Msg.Save
MsgBox Msg.Subject
You can then use the message (RDOMail) to access it various properties (Subject, Body, etc.)
Also keep in mind no Office app (Excel, Word, or Outlook) can be used from a Windows service (such as IIS).
Solution 2:[2]
Thanks to Dmitry I managed to find a solution. I ended up using Independentsoft.Msg nuget package. The code is here:
Message message = new Message(fileToClean.InputFileName);
var attachmentList = new List<Attachment>();
// First check all attachments, and add the clean ones to the attachment list
foreach (BodyPart bodyPart in message.BodyParts)
{
if ((bodyPart.ContentDisposition != null) &&
(bodyPart.ContentDisposition.Type == ContentDispositionType.Attachment))
{
foreach (Attachment attachment in message.GetAttachments())
{
if (attachment.GetFileName() == bodyPart.ContentDisposition.Parameters[0].Value)
{
if (IsClean(attachment, fileToClean))
{
attachmentList.Add(attachment);
}
break;
}
}
}
}
// Remove all attachements
message.BodyParts.RemoveAll(bp =>
(bp.ContentDisposition != null) &&
(bp.ContentDisposition.Type ==
ContentDispositionType.Attachment));
// Attach Cleaned attachments
foreach (Attachment attachment in attachmentList)
{
message.BodyParts.Add(new BodyPart(attachment));
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Andre Grobler |