Skip to content

Commit

Permalink
Added support for tnef winmail.dat attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
Kees van Spelde committed Mar 26, 2023
1 parent 2124ee9 commit 9bc91cf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
3 changes: 1 addition & 2 deletions MsgReaderCore/Mime/Traverse/AttachmentFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ protected override List<MessagePart> CaseLeaf(MessagePart messagePart)
try
{
Logger.WriteToLog("Found winmail.dat attachment, trying to get attachments from it");
var stream = StreamHelpers.Manager.GetStream("AttachmentFinder.CaseLeaf", messagePart.Body, 0,
messagePart.Body.Length);
var stream = StreamHelpers.Manager.GetStream("AttachmentFinder.CaseLeaf", messagePart.Body, 0, messagePart.Body.Length);
using var tnefReader = new TnefReader(stream);
{
var attachments = Part.ExtractAttachments(tnefReader);
Expand Down
2 changes: 1 addition & 1 deletion MsgReaderCore/Outlook/Attachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ internal Attachment(Tnef.Attachment attachment)
RenderingPosition = -1;
_data = attachment.Body;
CreationTime = attachment.CreationDate;
MimeType = Helpers.MimeType
FileName = attachment.FileName;
MimeType = MimeTypes.GetMimeType(FileName);
StorageName = null;
}

Expand Down
42 changes: 36 additions & 6 deletions MsgReaderCore/Outlook/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
using MsgReader.Localization;
using MsgReader.Mime.Header;
using MsgReader.Rtf;
using MsgReader.Tnef;
using OpenMcdf;

// ReSharper disable StringLiteralTypo
Expand Down Expand Up @@ -1682,8 +1683,7 @@ private void LoadEncryptedAndPossibleSignedMessage(CFStorage storage)
var attachment = new Attachment(new Storage(storage), null);

if (attachment.FileName.ToUpperInvariant() != "SMIME.P7M")
throw new MRInvalidSignedFile(
"The signed file is not valid, it should contain an attachment called smime.p7m but it didn't");
throw new MRInvalidSignedFile("The signed file is not valid, it should contain an attachment called smime.p7m but it didn't");

ProcessSignedContent(attachment.Data);
}
Expand Down Expand Up @@ -1757,7 +1757,38 @@ private void LoadAttachmentStorage(CFStorage storage, string storageName)

default:
// Add attachment to attachment list
_attachments.Add(attachment);
if (attachment.FileName.ToLowerInvariant() == "winmail.dat")
{
try
{
Logger.WriteToLog("Found winmail.dat attachment, trying to get attachments from it");
var stream = StreamHelpers.Manager.GetStream("Message.LoadAttachmentStorage", attachment.Data, 0, attachment.Data.Length);
using var tnefReader = new TnefReader(stream);
{
var tnefAttachments = Part.ExtractAttachments(tnefReader);
var count = tnefAttachments.Count;
if (count > 0)
{
Logger.WriteToLog($"Found {count} attachment{(count == 1 ? string.Empty : "s")}, removing winmail.dat and adding {(count == 1 ? "this attachment" : "these attachments")}");

foreach (var tnefAttachment in tnefAttachments)
{
var temp = new Attachment(tnefAttachment);
_attachments.Add(temp);
}
}
}

}
catch (Exception exception)
{
Logger.WriteToLog($"Could not parse winmail.dat attachment, error: {ExceptionHelpers.GetInnerException(exception)}");
_attachments.Add(attachment);
}
}
else
_attachments.Add(attachment);

break;
}

Expand Down Expand Up @@ -1998,6 +2029,7 @@ private void SetEmailSenderAndRepresentingSender()
var parts = tempDisplayName.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 0)
{
// ReSharper disable once UseIndexFromEndExpression
var lastPart = parts[parts.Length - 1];
tempDisplayName = lastPart.Contains("=") ? lastPart.Split('=')[1] : lastPart;
}
Expand Down Expand Up @@ -2029,9 +2061,7 @@ private void SetEmailSenderAndRepresentingSender()
var representingAddressType = GetMapiPropertyString(MapiTags.PR_SENT_REPRESENTING_ADDRTYPE);
tempEmail = GetMapiPropertyString(MapiTags.PR_SENT_REPRESENTING_EMAIL_ADDRESS);
tempEmail = EmailAddress.RemoveSingleQuotes(tempEmail);
tempDisplayName =
EmailAddress.RemoveSingleQuotes(GetMapiPropertyString(MapiTags.PR_SENT_REPRESENTING_NAME));

tempDisplayName = EmailAddress.RemoveSingleQuotes(GetMapiPropertyString(MapiTags.PR_SENT_REPRESENTING_NAME));
email = tempEmail;
displayName = tempDisplayName;

Expand Down

0 comments on commit 9bc91cf

Please sign in to comment.