Skip to content

Commit

Permalink
Try another method to resolve address
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Jul 3, 2023
1 parent 1074326 commit 73504de
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 25 deletions.
88 changes: 66 additions & 22 deletions Dialog/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.Threading.Tasks;
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Graph;
using Microsoft.Identity.Client;
using Azure.Identity;

namespace FlexConfirmMail.Dialog
{
Expand All @@ -14,8 +18,12 @@ internal class RecipientInfo : IComparable
public const string DOMAIN_EXCHANGE = "Exchange";
public const string DOMAIN_EXCHANGE_EXT = "Exchange (ext)";

public RecipientInfo(Outlook.Recipient recp)
private static Outlook.Recipient recp { get; set; }

public RecipientInfo(Outlook.Recipient recipient)
{
recp = recipient;

recp.Resolve();
QueueLogger.Log("RecipientInfo");
QueueLogger.Log($" Resolved: {recp.Resolved}");
Expand All @@ -26,90 +34,126 @@ public RecipientInfo(Outlook.Recipient recp)
QueueLogger.Log($" AddressEntry.Address: {recp.AddressEntry.Address}");
QueueLogger.Log($" AddressEntry.DisplayType: {recp.AddressEntry.DisplayType}");
QueueLogger.Log($" AddressEntry.Type: {recp.AddressEntry.Type}");

if (recp.AddressEntry.DisplayType == Outlook.OlDisplayType.olUser
&& recp.AddressEntry.Type == "SMTP")
{
FromSMTP(recp);
FromSMTP();
}
else if (recp.AddressEntry.DisplayType == Outlook.OlDisplayType.olUser ||
recp.AddressEntry.DisplayType == Outlook.OlDisplayType.olRemoteUser)
{
FromExchange(recp);
FromExchange();
}
else if (recp.AddressEntry.DisplayType == Outlook.OlDisplayType.olPrivateDistList ||
recp.AddressEntry.DisplayType == Outlook.OlDisplayType.olDistList)
{
FromDistList(recp);
FromDistList();
}
else
{
FromOther(recp);
FromOther();
}
}

private void FromSMTP(Outlook.Recipient recp)
private void FromSMTP()
{
QueueLogger.Log(" => FromSMTP");
Type = GetType(recp);
Type = GetType();
Address = recp.Address;
Domain = GetDomainFromSMTP(Address);
Help = Address;
IsSMTP = true;
}

private void FromExchange(Outlook.Recipient recp)
private void FromExchange()
{
FromExchangeAsync().Wait();
}
private async Task<bool> FromExchangeAsync()
{
QueueLogger.Log(" => FromExchange");
Outlook.ExchangeUser user = recp.AddressEntry.GetExchangeUser();
QueueLogger.Log($" user: {user}");
if (user == null)
{
FromOther(recp);
return;
FromOther();
return true;
}

string PossibleAddress = user.PrimarySmtpAddress;
if (string.IsNullOrEmpty(PossibleAddress))
{
QueueLogger.Log(" PrimarySmtpAddress is blank: trying to get it via PropertyAccessor");
const string PR_SMTP_ADDRESS = "https://schemas.microsoft.com/mapi/proptag/0x39FE001E";
PossibleAddress = recp.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS).ToString();
QueueLogger.Log(" PrimarySmtpAddress is blank: trying to get it via Microsoft Graph API");
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
//var scopes = new[] { "User.Read" };
//var options = new DeviceCodeCredentialOptions
//{
// AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
// ClientId = "<UUID>",
// TenantId = "common"
//};
//var credential = new DeviceCodeCredential(options);
//var graphClient = new GraphServiceClient(credential, scopes);
var ClientId = "<UUID>";
var TenantId = "common";
var ClientSecret = "<secret>";
var scopes = new[] {"https://graph.microsoft.com/.default"};
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
var credential = new ClientSecretCredential(TenantId, ClientId, ClientSecret, options);
var graphClient = new GraphServiceClient(credential, scopes);
try
{
QueueLogger.Log($" user.ID: {user.ID}");
var result = await graphClient.Users[user.ID].GetAsync().ConfigureAwait(false);
QueueLogger.Log($" result: {result}");
PossibleAddress = result.UserPrincipalName;
if (string.IsNullOrEmpty(PossibleAddress))
{
QueueLogger.Log(" Couldn't get address");
FromOther(recp);
return;
FromOther();
return true;
}
}
catch(Microsoft.Graph.Models.ODataErrors.ODataError e)
{
QueueLogger.Log($" error: code={e.Error.Code}, message={e.Error.Message}, details={e.Error.Details}");
}
}
QueueLogger.Log($" => finally resolved addrss: {PossibleAddress}");

Type = GetType(recp);
Type = GetType();
Address = PossibleAddress;
Domain = GetDomainFromSMTP(Address);
Help = Address;
IsSMTP = true;

return true;
}

private void FromDistList(Outlook.Recipient recp)
private void FromDistList()
{
QueueLogger.Log(" => FromDistList");
Outlook.ExchangeDistributionList dist = recp.AddressEntry.GetExchangeDistributionList();
QueueLogger.Log($" dist: {dist}");
if (dist == null || string.IsNullOrEmpty(dist.PrimarySmtpAddress))
{
FromOther(recp);
FromOther();
}
else
{
Type = GetType(recp);
Type = GetType();
Address = dist.PrimarySmtpAddress;
Domain = GetDomainFromSMTP(Address);
Help = Address;
IsSMTP = true;
}
}

private void FromOther(Outlook.Recipient recp)
private void FromOther()
{
QueueLogger.Log($" => FromOther ({recp.AddressEntry.DisplayType})");
switch (recp.AddressEntry.DisplayType)
Expand All @@ -129,7 +173,7 @@ private void FromOther(Outlook.Recipient recp)
break;
}

Type = GetType(recp);
Type = GetType();
Address = recp.Name;
Help = $"[{Domain}] {Address}";
IsSMTP = false;
Expand All @@ -140,7 +184,7 @@ private string GetDomainFromSMTP(string addr)
return addr.Substring(addr.IndexOf('@') + 1).ToLower();
}

private static string GetType(Outlook.Recipient recp)
private static string GetType()
{
switch (recp.Type)
{
Expand Down
7 changes: 6 additions & 1 deletion Dialog/MainDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Threading.Tasks;
using Outlook = Microsoft.Office.Interop.Outlook;


namespace FlexConfirmMail.Dialog
{
public partial class MainDialog : Window
Expand Down Expand Up @@ -49,11 +51,14 @@ public bool SkipConfirm()
private void RenderMain()
{
List<RecipientInfo> recipients = new List<RecipientInfo>();
RecipientInfo info = null;

foreach (Outlook.Recipient recp in _mail.Recipients)
{
recipients.Add(new RecipientInfo(recp));
info = new RecipientInfo(recp);
recipients.Add(info);
}
QueueLogger.Log("initializing...");

// Address CheckBox
recipients.Sort();
Expand Down
9 changes: 7 additions & 2 deletions FlexConfirmMail.iss
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ Name: jp; MessagesFile: "compiler:Languages\Japanese.isl"
[Files]
Source: "bin\Release\FlexConfirmMail.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "bin\Release\FlexConfirmMail.dll.manifest"; DestDir: "{app}"; Flags: ignoreversion
Source: "bin\Release\FlexConfirmMail.dll.config"; DestDir: "{app}"; Flags: ignoreversion
Source: "bin\Release\FlexConfirmMail.vsto"; DestDir: "{app}"; Flags: ignoreversion
Source: "bin\Release\en\FlexConfirmMail.resources.dll"; DestDir: "{app}\en"; Flags: ignoreversion
Source: "bin\Release\zh\FlexConfirmMail.resources.dll"; DestDir: "{app}\zh"; Flags: ignoreversion
Source: "bin\Release\Microsoft.Office.Tools.Common.v4.0.Utilities.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "bin\Release\Microsoft.Office.Tools.Outlook.v4.0.Utilities.dll"; DestDir: "{app}"; Flags: ignoreversion
;Source: "bin\Release\Microsoft.Office.Tools.Common.v4.0.Utilities.dll"; DestDir: "{app}"; Flags: ignoreversion
;Source: "bin\Release\Microsoft.Office.Tools.Outlook.v4.0.Utilities.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "bin\Release\Azure.*.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "bin\Release\Microsoft.*.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "bin\Release\System.*.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "bin\Release\Tavis.*.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "Resources\fcm.ico"; DestDir: "{app}"; Flags: ignoreversion

0 comments on commit 73504de

Please sign in to comment.