-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThisAddIn.cs
More file actions
127 lines (120 loc) · 5.66 KB
/
ThisAddIn.cs
File metadata and controls
127 lines (120 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Diagnostics;
namespace WhyMe
{
public partial class ThisAddIn
{
private Outlook.AddressEntries addrEntries;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
// Note: Outlook no longer raises this event. If you have code that
// must run when Outlook shuts down, see http://go.microsoft.com/fwlink/?LinkId=506785
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
public class Membership
{
public bool IsDirect { get; set; }
public IList<string> MemberOfDLs { get; set; }
public override string ToString()
{
if (IsDirect)
{
return "Mail was sent directly To/CC you.";
}
else
{
return "Found you in " + string.Join("\r\n-> ", MemberOfDLs);
}
}
}
public Membership IsMemberOf(Outlook.AddressEntry groupname)
{
Membership ret = null;
try
{
Outlook.Application a = new Outlook.Application();
Outlook.AddressEntry currentUser = a.Session.CurrentUser.AddressEntry;
if (currentUser.Type == "EX")
{
Outlook.ExchangeUser exchUser = currentUser.GetExchangeUser();
if (exchUser != null)
{
if (addrEntries == null)
addrEntries = exchUser.GetMemberOfList();
if (addrEntries != null)
{
if (groupname.Name.Equals(currentUser.Name)) //User is on To/CC
{
ret = new Membership();
ret.IsDirect = true;
return ret;
// return "Mail was sent directly To/CC you.";
}
else
{
Debug.WriteLine("Searching for " + groupname.Name + " in your groups...");
foreach (Outlook.AddressEntry addrEntry in addrEntries) //See if this group is in the user's groups list
{
if (addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry)
if (addrEntry.Name.Equals(groupname.Name))
{
ret = new Membership();
ret.MemberOfDLs = new List<string>();
ret.MemberOfDLs.Add(addrEntry.Name);
return ret;
// return "Found you in " + addrEntry.Name;
}
}
if (groupname.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry) //If not, recurse into member-DLs
{
Outlook.ExchangeDistributionList exchDL = groupname.GetExchangeDistributionList();
Outlook.AddressEntries subAddrEntries = exchDL.GetExchangeDistributionListMembers();
if (subAddrEntries != null)
{
foreach (Outlook.AddressEntry exchDLMember in subAddrEntries)
{
if (exchDLMember.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry)
{
Debug.WriteLine("Attempting to recurse into " + exchDLMember.Name + "(child of " + groupname.Name + ")...");
ret = IsMemberOf(exchDLMember);
if (ret != null)
{
ret.MemberOfDLs.Add(groupname.Name);
break;
}
}
}
}
}
}
}
}
}
}
catch (Exception e)
{
Debug.WriteLine("Exception: " + e);
}
return ret;
}
}
}