-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeProcessor.cs
115 lines (97 loc) · 4.06 KB
/
MergeProcessor.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
using System.Linq;
using System.ComponentModel.DataAnnotations;
using Microsoft.Graph;
namespace MailMerge
{
public class MergeProcessor
{
static SemaphoreSlim throttler = new SemaphoreSlim(initialCount: 1);
public GraphServiceClient Client { get; set;}
public bool MergeValid { get => MergeExceptions.Count == 0 ? true : false; }
public bool SendValid { get => SendExceptions.Count == 0 ? true : false; }
public bool Sending { get; set; }
public List<Exception> MergeExceptions { get; private set; }
public List<Exception> SendExceptions { get; private set; }
public MailMergeModel Template { get; }
public List<MailMergedRecord> MergedRecords { get; } = new List<MailMergedRecord>();
private MergeProcessor(GraphServiceClient client, MailMergeModel template)
{
Client = client;
Template = template;
MergeExceptions = new List<Exception>();
if (String.IsNullOrWhiteSpace(Template.EmailSubject))
MergeExceptions.Add(new ArgumentException($"Email Subject Is Empty"));
if (String.IsNullOrWhiteSpace(Template.EmailBody))
MergeExceptions.Add(new ArgumentException($"Email Body Is Empty"));
if (Template.ToField is null)
MergeExceptions.Add(new ArgumentException($"Email To: is Empty"));
SendExceptions = new List<Exception>();
}
public static async Task<MergeProcessor> Create(GraphServiceClient client, MailMergeModel template)
{
var merge = new MergeProcessor(client, template);
await merge.Initialize();
return merge;
}
private async Task Initialize()
{
ArgumentNullException.ThrowIfNull(Template?.Table?.Rows);
if (MergeExceptions.Count > 0)
return;
foreach (var row in Template.Table.Rows)
{
var merge = await MailMergedRecord.Create(Template, row);
MergedRecords.Add(merge);
MergeExceptions.AddRange(merge.Exceptions);
}
}
List<System.Threading.Tasks.Task> tasks = new List<System.Threading.Tasks.Task>();
public async Task Send(IProgress<string>? progress = null)
{
Sending = true;
try
{
SendExceptions = new List<Exception>();
if (!MergeValid)
{
progress?.Report($"Invalid Merge");
throw new Exception("Invalid Merge");
}
int sent = 0;
int failed = 0;
int total = MergedRecords.Count();
foreach(var record in MergedRecords)
{
await throttler.WaitAsync();
tasks.Add(System.Threading.Tasks.Task.Run(async () =>
{
try
{
await Client.Me
.SendMail(record.GetMessage(), true)
.Request()
.PostAsync();
sent++;
}
catch (System.Exception ex)
{
failed++;
SendExceptions.Add(ex);
}
finally
{
throttler.Release();
progress?.Report($"Sent {sent} of {total}. {failed} Failed");
}
}));
progress?.Report($"Complete {sent} of {total}. {failed} Failed");
}
}
finally
{
Sending = false;
}
return;
}
}
}