-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailMergedRecord.cs
183 lines (158 loc) · 6.11 KB
/
MailMergedRecord.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System.ComponentModel.DataAnnotations;
using Microsoft.Graph;
using System.Text.RegularExpressions;
using System.Text.Json.Nodes;
using System.Text.Json;
using System.Web;
namespace MailMerge;
public partial class MailMergedRecord
{
public static async Task<MailMergedRecord> Create(MailMergeModel template, WorkbookTableRow row)
{
var merge = new MailMergedRecord(template, row);
await merge.Initialize();
return merge;
}
[GeneratedRegex(@"\(\(.*?\)\)", RegexOptions.CultureInvariant, matchTimeoutMilliseconds: 10000)]
private static partial Regex MergeFields();
private static EmailAddressAttribute emailValid = new EmailAddressAttribute();
public List<Exception> Exceptions { get; private set; }
private MailMergedRecord(MailMergeModel template, WorkbookTableRow row)
{
Row = row;
Template = template;
Exceptions = new List<Exception>();
}
private async Task Initialize()
{
//possibly async in future
await Task.Yield();
object[][] rowvaluesx = JsonSerializer.Deserialize<object[][]>(Row.Values)!;
object[] rowvalues = rowvaluesx[0];
if (Template.From is null || !emailValid.IsValid(Template.From))
Exceptions.Add(new ArgumentException($"Invalid From: Email {Template.From} Row: {Row.Index + 1}"));
else
From = Template.From;
if (Template.ToField is not null)
To = AddAddresses(CellValue(Template.ToField, rowvalues), Row.Index, true);
else
Exceptions.Add(new ArgumentException($"Missing To: Email Address Row: {Row.Index + 1}"));
if (Template.CcField is not null)
Cc = AddAddresses(CellValue(Template.CcField, rowvalues), Row.Index);
if (Template.BccField is not null)
Bcc = AddAddresses(CellValue(Template.BccField, rowvalues), Row.Index);
if (Template.EmailBody is not null)
MergedBody = MergeFields().Replace(Template.EmailBody, delegate(Match match)
{
string key = match.Groups[0].Value;
return CellValue(key.Substring(2,key.Length-4), rowvalues);
});
else
Exceptions.Add(new ArgumentException($"Email Body Is Empty"));
if (Template.EmailSubject is not null)
MergedSubject = MergeFields().Replace(Template.EmailSubject, delegate(Match match)
{
string key = match.Groups[0].Value;
return CellValue(key.Substring(2,key.Length-4), rowvalues);
});
else
Exceptions.Add(new ArgumentException($"Subject Is Empty"));
}
public string CellValue(string columnName, object[] row)
{
ArgumentNullException.ThrowIfNull(Template?.Table?.Columns);
var column = Template.Table.Columns.Where(x => x.Name == columnName).SingleOrDefault();
if (column is null)
{
Exceptions.Add(new ArgumentException($"Column {columnName} Not Found"));
return String.Empty;
}
return HttpUtility.HtmlEncode(row[Template.Table.Columns.IndexOf(column)]?.ToString() ?? String.Empty);
}
public string CellValue(WorkbookTableColumn column, object[] row)
{
ArgumentNullException.ThrowIfNull(Template?.Table?.Columns);
return HttpUtility.HtmlEncode(row[Template.Table.Columns.IndexOf(column)]?.ToString() ?? String.Empty);
}
public MailMergeModel Template { get; private set;}
public WorkbookTableRow Row { get; private set;}
public string From { get; private set; } = null!;
public List<Recipient> To { get; private set;} = null!;
public List<Recipient> Cc { get; private set;} = null!;
public List<Recipient> Bcc { get; private set;} = null!;
public string MergedBody { get; set;} = null!;
public string MergedSubject { get; set;} = null!;
public bool Valid { get => Exceptions.Count == 0 ? true : false; }
public string ToList
{
get
{
if (To is null)
return String.Empty;
string tmp = "";
foreach (var ad in To)tmp += ad.EmailAddress.Address + ";";
return tmp;
}
}
public string CcList
{
get
{
if (Cc is null)
return String.Empty;
string tmp = "";
foreach (var ad in Cc)tmp += ad.EmailAddress.Address+ ";";
return tmp;
}
}
public string BccList
{
get
{
if (Bcc is null)
return String.Empty;
string tmp = "";
foreach (var ad in Bcc)tmp += ad.EmailAddress.Address+ ";";
return tmp;
}
}
public Microsoft.Graph.Message GetMessage()
{
return new Microsoft.Graph.Message
{
Subject = MergedSubject,
Body = new ItemBody
{
ContentType = Microsoft.Graph.BodyType.Html,
Content = MergedBody
},
ToRecipients = To,
CcRecipients = Cc,
BccRecipients = Bcc
};
}
private List<Recipient> AddAddresses(string addresses, int? RowIndex, bool required = false)
{
List<Recipient> recipients = new List<Recipient>();
foreach (var address in addresses.Split(';').Select(p => p.Trim()))
{
if (emailValid.IsValid(address))
{
recipients.Add(new Recipient
{
EmailAddress = new Microsoft.Graph.EmailAddress
{
Address = address
}
});
}
else
{
Exceptions.Add(new ArgumentException($"Invalid Email {address} Row: {Row.Index + 1}"));
}
}
if (required && recipients.Count == 0)
Exceptions.Add(new ArgumentException($"Missing Required Email Address Row: {Row.Index + 1}"));
return recipients;
}
}