Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions MsgReader/Outlook/Message.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Message.cs
//
// Author: Kees van Spelde <sicos2002@hotmail.com>
Expand Down Expand Up @@ -2416,7 +2416,30 @@ public string GetEmailRecipientsRfc822Format(RecipientType type)

var recipients = GetEmailRecipients(type);
if (Appointment?.UnsendableRecipients != null)
recipients.AddRange(Appointment.UnsendableRecipients.GetEmailRecipients(type));
{
try
{
recipients.AddRange(Appointment.UnsendableRecipients.GetEmailRecipients(type));
}
catch
{
//ignore
}
}

// When a recipient only has a display name and no e-mail address (this can happen when
// a recipient was resolved against an address book but the string-based MAPI properties
// like PR_EMAIL_ADDRESS/PR_SMTP_ADDRESS were never persisted), try to recover the address
// from the parsed Internet transport headers by matching on display name.
List<RfcMailAddress> headerAddresses = null;
if (Headers != null)
headerAddresses = type switch
{
RecipientType.To => Headers.To,
RecipientType.Cc => Headers.Cc,
RecipientType.Bcc => Headers.Bcc,
_ => null
};

foreach (var recipient in recipients)
{
Expand All @@ -2428,12 +2451,24 @@ public string GetEmailRecipientsRfc822Format(RecipientType type)
if (!string.IsNullOrEmpty(recipient.DisplayName))
tempOutput += "\"" + recipient.DisplayName + "\"";

if (!string.IsNullOrEmpty(recipient.Email))
var email = recipient.Email;

if (string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(recipient.DisplayName) && headerAddresses != null)
{
var match = headerAddresses.FirstOrDefault(address =>
!string.IsNullOrEmpty(address.DisplayName) &&
string.Equals(address.DisplayName, recipient.DisplayName, StringComparison.OrdinalIgnoreCase));

if (match != null && !string.IsNullOrEmpty(match.Address))
email = match.Address;
}

if (!string.IsNullOrEmpty(email))
{
if (!string.IsNullOrEmpty(tempOutput))
tempOutput += " ";

tempOutput += "<" + recipient.Email + ">";
tempOutput += "<" + email + ">";
}

output += tempOutput;
Expand Down Expand Up @@ -2464,8 +2499,17 @@ public string GetEmailRecipients(RecipientType type,

var recipients = GetEmailRecipients(type);
if (Appointment?.UnsendableRecipients != null)
recipients.AddRange(Appointment.UnsendableRecipients.GetEmailRecipients(type));

{
try
{
recipients.AddRange(Appointment.UnsendableRecipients.GetEmailRecipients(type));
}
catch
{
//ignore
}
}

foreach (var recipient in recipients)
{
if (output != string.Empty)
Expand Down Expand Up @@ -2584,4 +2628,4 @@ public List<string> GetOwnerReactionStringList()
}
#endregion
}
}
}
24 changes: 22 additions & 2 deletions MsgReader/Outlook/Recipient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Recipient.cs
//
// Author: Kees van Spelde <sicos2002@hotmail.com>
Expand All @@ -25,6 +25,7 @@
//

using System;
using System.Text;
using MsgReader.Helpers;

namespace MsgReader.Outlook;
Expand Down Expand Up @@ -174,6 +175,25 @@ internal Recipient(Storage message) : base(message._rootStorage)
if (!string.IsNullOrEmpty(testEmail) && testEmail.Contains("@")) tempEmail = testEmail;
}

// As a last resort, some recipients (typically resolved against an Exchange address book)
// never get PR_EMAIL_ADDRESS, PR_SMTP_ADDRESS or PR_ORGEMAILADDR populated, but still carry
// a usable address inside PR_SEARCH_KEY. This is stored as an ASCII, null-terminated string
// prefixed with the address type, e.g. "SMTP:name@domain.com"
if (string.IsNullOrEmpty(tempEmail) || !tempEmail.Contains("@"))
{
var searchKey = GetMapiPropertyBytes(MapiTags.PR_SEARCH_KEY);
if (searchKey != null)
{
var searchKeyString = Encoding.ASCII.GetString(searchKey).TrimEnd('\0');
if (searchKeyString.StartsWith("SMTP:", StringComparison.OrdinalIgnoreCase))
{
var testEmail = searchKeyString.Substring(5);
if (!string.IsNullOrEmpty(testEmail) && testEmail.Contains("@"))
tempEmail = testEmail;
}
}
}

tempEmail = EmailAddress.RemoveSingleQuotes(tempEmail);
var tempDisplayName = EmailAddress.RemoveSingleQuotes(GetMapiPropertyString(MapiTags.PR_DISPLAY_NAME));

Expand Down Expand Up @@ -220,4 +240,4 @@ internal Recipient(Storage message) : base(message._rootStorage)
}
#endregion
}
}
}
Loading