-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathOutlook.cs
145 lines (131 loc) · 3.94 KB
/
Outlook.cs
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using TypeAgent.Core;
namespace TypeAgent;
public class Outlook : COMObject
{
private Application _outlook;
private NameSpace _session;
public Outlook()
{
_outlook = new Application();
_session = _outlook.Session;
}
public List<Email> LoadFrom(EmailSender sender)
{
Filter filter = sender.ToFilter();
return FilterItems(filter, (item) =>
{
return item is MailItem mailItem ? new Email(mailItem) : null;
});
}
public Email LoadEmail(string filePath)
{
Verify.FileExists(filePath);
MailItem mail = (MailItem)_session.OpenSharedItem(filePath);
try
{
return new Email(mail, filePath);
}
finally
{
COMObject.Release(mail);
mail = null;
}
}
public List<T> FilterItems<T>(Filter filter, Func<object, T> gettor) where T : class
{
NameSpace ns = null;
MAPIFolder inbox = null;
Items items = null;
Items filteredItems = null;
try
{
ns = _outlook.GetNamespace("MAPI");
inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
items = inbox.Items;
filteredItems = items.Restrict(filter);
List<T> typedItems = new List<T>();
foreach (object item in filteredItems)
{
T itemT = gettor(item);
if (itemT != null)
{
typedItems.Add(itemT);
}
}
return typedItems;
}
finally
{
COMObject.Release(filteredItems);
COMObject.Release(items);
COMObject.Release(inbox);
COMObject.Release(ns);
}
}
public IEnumerable<T> MapMailItems<T>(Func<MailItem, T> mapFn, OlSensitivity sensitivity = OlSensitivity.olNormal)
{
return MapMailItems<T>(mapFn, null, sensitivity);
}
public IEnumerable<T> MapMailItems<T>(Func<MailItem, T> mapFn, Filter? filter, OlSensitivity sensitivity = OlSensitivity.olNormal)
{
foreach(MailItem item in ForEachMailItem(filter, sensitivity))
{
T result = mapFn(item);
yield return result;
}
}
public IEnumerable<MailItem> ForEachMailItem(OlSensitivity sensitivity = OlSensitivity.olNormal)
{
return ForEachMailItem(null, sensitivity);
}
public IEnumerable<MailItem> ForEachMailItem(Filter? filter, OlSensitivity sensitivity = OlSensitivity.olNormal)
{
NameSpace ns = null;
MAPIFolder inbox = null;
Items items = null;
Items filteredItems = null;
try
{
ns = _outlook.GetNamespace("MAPI");
inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
items = inbox.Items;
if (filter is not null)
{
filteredItems = items.Restrict(filter);
}
items.Sort("[ReceivedTime]", true);
foreach (object item in filteredItems ?? items)
{
try
{
if (item is MailItem mailItem &&
mailItem.HasBody() &&
mailItem.Sensitivity == sensitivity)
{
yield return mailItem;
}
}
finally
{
COMObject.Release(item);
}
}
}
finally
{
COMObject.Release(filteredItems);
COMObject.Release(items);
COMObject.Release(inbox);
COMObject.Release(ns);
}
}
protected override void OnDispose()
{
COMObject.Release(_session);
COMObject.Release(_outlook);
_session = null;
_outlook = null;
}
}