Skip to content

Commit 1231a53

Browse files
authored
Merge pull request #13 from FlexConfirmMail/resolve-recipient
Resolve recipient addresses of suggested Exchange users
2 parents aa6e66a + 0988e87 commit 1231a53

File tree

4 files changed

+81
-13
lines changed

4 files changed

+81
-13
lines changed

Dialog/Helper.cs

+75-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text.RegularExpressions;
23
using Outlook = Microsoft.Office.Interop.Outlook;
34

45
namespace FlexConfirmMail.Dialog
@@ -16,6 +17,16 @@ internal class RecipientInfo : IComparable
1617

1718
public RecipientInfo(Outlook.Recipient recp)
1819
{
20+
recp.Resolve();
21+
QueueLogger.Log("RecipientInfo");
22+
QueueLogger.Log($" Resolved: {recp.Resolved}");
23+
QueueLogger.Log($" Name: {recp.Name}");
24+
QueueLogger.Log($" Type: {recp.Type}");
25+
QueueLogger.Log($" Address: {recp.Address}");
26+
QueueLogger.Log($" AddressEntry.Name: {recp.AddressEntry.Name}");
27+
QueueLogger.Log($" AddressEntry.Address: {recp.AddressEntry.Address}");
28+
QueueLogger.Log($" AddressEntry.DisplayType: {recp.AddressEntry.DisplayType}");
29+
QueueLogger.Log($" AddressEntry.Type: {recp.AddressEntry.Type}");
1930
if (recp.AddressEntry.DisplayType == Outlook.OlDisplayType.olUser
2031
&& recp.AddressEntry.Type == "SMTP")
2132
{
@@ -39,6 +50,7 @@ public RecipientInfo(Outlook.Recipient recp)
3950

4051
private void FromSMTP(Outlook.Recipient recp)
4152
{
53+
QueueLogger.Log(" => FromSMTP");
4254
Type = GetType(recp);
4355
Address = recp.Address;
4456
Domain = GetDomainFromSMTP(Address);
@@ -48,24 +60,79 @@ private void FromSMTP(Outlook.Recipient recp)
4860

4961
private void FromExchange(Outlook.Recipient recp)
5062
{
63+
QueueLogger.Log(" => FromExchange");
5164
Outlook.ExchangeUser user = recp.AddressEntry.GetExchangeUser();
52-
if (user == null || string.IsNullOrEmpty(user.PrimarySmtpAddress))
65+
QueueLogger.Log($" user: {user}");
66+
67+
string possibleAddress = "";
68+
if (user == null ||
69+
string.IsNullOrEmpty(user.PrimarySmtpAddress))
5370
{
54-
FromOther(recp);
71+
QueueLogger.Log(" user is null or has no PrimarySmtpAddress: trying to get it via PropertyAccessor");
72+
const string PR_SMTP_ADDRESS = "https://schemas.microsoft.com/mapi/proptag/0x39FE001E";
73+
possibleAddress = GetSMTPAddressViaAccessor(recp, PR_SMTP_ADDRESS);
74+
if (string.IsNullOrEmpty(possibleAddress))
75+
{
76+
const string PR_EMS_PROXY_ADDRESSES = "http://schemas.microsoft.com/mapi/proptag/0x800f101e";
77+
possibleAddress = GetSMTPAddressViaAccessor(recp, PR_EMS_PROXY_ADDRESSES);
78+
}
5579
}
5680
else
5781
{
58-
Type = GetType(recp);
59-
Address = user.PrimarySmtpAddress;
60-
Domain = GetDomainFromSMTP(Address);
61-
Help = Address;
62-
IsSMTP = true;
82+
possibleAddress = user.PrimarySmtpAddress;
83+
}
84+
85+
if (string.IsNullOrEmpty(possibleAddress))
86+
{
87+
QueueLogger.Log(" Couldn't get address: fallback to FromOther");
88+
FromOther(recp);
89+
return;
90+
}
91+
QueueLogger.Log($" => finally resolved addrss: {possibleAddress}");
92+
93+
Type = GetType(recp);
94+
Address = possibleAddress;
95+
Domain = GetDomainFromSMTP(Address);
96+
Help = Address;
97+
IsSMTP = true;
98+
}
99+
100+
private string GetSMTPAddressViaAccessor(Outlook.Recipient recp, string schemaName)
101+
{
102+
try
103+
{
104+
QueueLogger.Log($" Retrieving values for {schemaName}...");
105+
dynamic propertyValue = recp.AddressEntry.PropertyAccessor.GetProperty(schemaName);
106+
if (propertyValue is string[] values)
107+
{
108+
foreach (string value in values)
109+
{
110+
QueueLogger.Log($" value: {value}");
111+
// The recipient may have multiple values with their types like:
112+
// SIP:local@domain
113+
// SMTP:local@domain
114+
// We should accept only SMTP address.
115+
if (!string.IsNullOrEmpty(value) &&
116+
Regex.IsMatch(value, "^(SMTP:)?[^:@]+@.+", RegexOptions.IgnoreCase))
117+
{
118+
return Regex.Replace(value, "^SMTP:", "");
119+
}
120+
}
121+
}
122+
return propertyValue.ToString();
123+
}
124+
catch (Exception ex)
125+
{
126+
QueueLogger.Log($" Failed to GetProperty with {schemaName}: {ex}");
127+
return "";
63128
}
64129
}
65130

66131
private void FromDistList(Outlook.Recipient recp)
67132
{
133+
QueueLogger.Log(" => FromDistList");
68134
Outlook.ExchangeDistributionList dist = recp.AddressEntry.GetExchangeDistributionList();
135+
QueueLogger.Log($" dist: {dist}");
69136
if (dist == null || string.IsNullOrEmpty(dist.PrimarySmtpAddress))
70137
{
71138
FromOther(recp);
@@ -82,6 +149,7 @@ private void FromDistList(Outlook.Recipient recp)
82149

83150
private void FromOther(Outlook.Recipient recp)
84151
{
152+
QueueLogger.Log($" => FromOther ({recp.AddressEntry.DisplayType})");
85153
switch (recp.AddressEntry.DisplayType)
86154
{
87155
case Outlook.OlDisplayType.olUser:

FlexConfirmMail.iss

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[Setup]
22
AppName=FlexConfirmMail
33
AppVerName=FlexConfirmMail
4-
VersionInfoVersion=22.3.0.0
4+
VersionInfoVersion=22.4.0
55
AppPublisher=ClearCode Inc.
6-
AppVersion=22.3.0
6+
AppVersion=22.4.0
77
UninstallDisplayIcon={app}\fcm.ico
88
DefaultDirName={commonpf}\FlexConfirmMail
99
ShowLanguageDialog=no

Global.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace FlexConfirmMail
55
public class Global
66
{
77
public static readonly string AppName = "FlexConfirmMail";
8-
public static readonly string Version = "22.3.0";
8+
public static readonly string Version = "22.4.0";
99
public static readonly string Edition = "Enterprise";
1010
public static readonly bool EnableGPO = true;
1111
}
12-
}
12+
}

Global.public.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace FlexConfirmMail
55
public class Global
66
{
77
public static readonly string AppName = "FlexConfirmMail";
8-
public static readonly string Version = "22.3.0";
8+
public static readonly string Version = "22.4.0";
99
public static readonly string Edition = "Free";
1010
public static readonly bool EnableGPO = false;
1111
}
12-
}
12+
}

0 commit comments

Comments
 (0)