-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathMessage_Variables.cs
More file actions
304 lines (269 loc) · 13.8 KB
/
Message_Variables.cs
File metadata and controls
304 lines (269 loc) · 13.8 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
namespace MailMergeLib.Tests;
[TestFixture]
class Message_Variables
{
[Test]
public void MissingVariableAndAttachmentsExceptions()
{
// build message with a total of 9 placeholders which will be missing
var mmm = new MailMergeMessage("Missing in subject {subject}", "Missing in plain text {plain}",
"<html><head></head><body>{html}{:template(Missing)}</body></html>");
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "{from.address}"));
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "{to.address}"));
mmm.AddExternalInlineAttachment(new FileAttachment("{inlineAtt.filename}.jpg", string.Empty));
mmm.FileAttachments.Add(new FileAttachment("{fileAtt.filename}.xml", "{fileAtt.displayname}"));
mmm.FileAttachments.Add(new FileAttachment("{throwParingError.xml", "DisplayName"));
// **************** Part 1:
mmm.Config.IgnoreMissingInlineAttachments = false;
mmm.Config.IgnoreMissingFileAttachments = false;
// ************************
try
{
mmm.GetMimeMessage(default);
Assert.Fail("Expected exceptions not thrown.");
}
catch (MailMergeMessage.MailMergeMessageException exceptions)
{
Console.WriteLine($"Aggregate {nameof(MailMergeMessage.MailMergeMessageException)} thrown.");
Console.WriteLine();
/* Expected exceptions:
* 1) 9 missing variables for {placeholders} and {:templates(...)}
* 2) No recipients
* 3) No FROM address
* 4) Missing file attachment {fileAtt.filename}.xml
* 5) Missing inline attachment {inlineAtt.filename}.jpg
* 6) Parsing error in file attachment "{throwParingError.xml"
*/
Assert.That(exceptions.InnerExceptions, Has.Count.EqualTo(6));
foreach (var ex in exceptions.InnerExceptions.Where(ex => ex is not MailMergeMessage.AttachmentException))
{
if (ex is MailMergeMessage.VariableException variableException)
{
Assert.That(variableException.MissingVariable, Has.Count.EqualTo(9));
Console.WriteLine($"{nameof(MailMergeMessage.VariableException)} thrown successfully:");
Console.WriteLine("Missing variables: " +
string.Join(", ",
variableException.MissingVariable));
Console.WriteLine();
}
if (ex is MailMergeMessage.AddressException addressException)
{
Console.WriteLine($"{nameof(MailMergeMessage.AddressException)} thrown successfully:");
Console.WriteLine(addressException.Message);
Console.WriteLine();
Assert.That(addressException.Message == "No recipients." ||
addressException.Message == "No from address.");
}
}
// one exception for a missing file attachment, one for a missing inline attachment
var attExceptions = exceptions.InnerExceptions.Where(ex => ex is MailMergeMessage.AttachmentException)
.ToList();
Assert.That(attExceptions, Has.Count.EqualTo(2));
// Inline file missing
Console.WriteLine($"{nameof(MailMergeMessage.AttachmentException)} thrown successfully:");
Console.WriteLine("Missing inline attachment files: " +
string.Join(", ", (attExceptions[0] as MailMergeMessage.AttachmentException)?.BadAttachment!));
Console.WriteLine();
Assert.That((attExceptions[0] as MailMergeMessage.AttachmentException)?.BadAttachment.Count, Is.EqualTo(1));
// 2 file attachments missing
Console.WriteLine($"{nameof(MailMergeMessage.AttachmentException)} thrown successfully:");
Console.WriteLine("Missing attachment files: " +
string.Join(", ", (attExceptions[1] as MailMergeMessage.AttachmentException)?.BadAttachment!));
Console.WriteLine();
Assert.That((attExceptions[1] as MailMergeMessage.AttachmentException)?.BadAttachment.Count, Is.EqualTo(2));
}
// **************** Part 2:
mmm.Config.IgnoreMissingInlineAttachments = true;
mmm.Config.IgnoreMissingFileAttachments = true;
// ************************
try
{
mmm.GetMimeMessage(default);
Assert.Fail("Expected exceptions not thrown.");
}
catch (MailMergeMessage.MailMergeMessageException exceptions)
{
/* Expected exceptions:
* 1) 9 missing variables for {placeholders} and {:templates(...)}
* 2) No recipients
* 3) No FROM address
* 4) 1 parsing error
*/
Assert.That(exceptions.InnerExceptions, Has.Count.EqualTo(4));
Assert.That(exceptions.InnerExceptions.Any(e => e is MailMergeMessage.AttachmentException), Is.False);
Console.WriteLine("Exceptions for missing attachment files suppressed.");
}
}
[Test]
public void MessagesFromDataRows()
{
using var tbl = new DataTable();
tbl.Columns.Add("Email", typeof(string));
tbl.Columns.Add("Continent", typeof(string));
tbl.Rows.Add("test@example.com", "Europe");
tbl.Rows.Add("2ndRow@axample.com", "Asia");
tbl.Rows.Add("3ndRow@axample.com", "America");
var text = "Lorem ipsum dolor. Email={Email}, Continent={Continent}.";
using var mmm = new MailMergeMessage("Subject for {Continent}", text);
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "from@example.com"));
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "{Email}"));
var i = 0;
foreach (var mimeMessage in mmm.GetMimeMessages<DataRow>(tbl.Rows.OfType<DataRow>()))
{
var row = tbl.Rows[i];
Assert.Multiple(() =>
{
Assert.That(mimeMessage.To.ToString().Contains(row["Email"].ToString()!), Is.True);
Assert.That(mimeMessage.TextBody!.Contains(text
.Replace("{Email}", row["Email"].ToString())
.Replace("{Continent}", row["Continent"].ToString())), Is.True);
});
MailMergeMessage.DisposeFileStreams(mimeMessage);
i++;
}
}
[Test]
public void MessagesFromListOfValueTuples()
{
var dataList = new List<ValueTuple<Dictionary<string, string>, Dictionary<string, string>>>();
var t1 = (new Dictionary<string, string> { { "Email", "test@example.com" } }, new Dictionary<string, string> { { "Continent", "Europe" } });
var t2 = (new Dictionary<string, string> { { "Email", "2ndRow@example.com" } }, new Dictionary<string, string> { { "Continent", "Asia" } });
var t3 = (new Dictionary<string, string> { { "Email", "3ndRow@example.com" } }, new Dictionary<string, string> { { "Continent", "America" } });
dataList.AddRange(new[] { t1, t2, t3 });
const string text = "Lorem ipsum dolor. Email={Email}, Continent={Continent}.";
using var mmm = new MailMergeMessage("Subject for {Continent}", text);
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "from@example.com"));
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "{Email}"));
var i = 0;
foreach (var mimeMessage in mmm.GetMimeMessages<ValueTuple<Dictionary<string, string>, Dictionary<string, string>>>(dataList))
{
var (emailPart, continentPart) = dataList[i];
Assert.Multiple(() =>
{
Assert.That(mimeMessage.To.ToString().Contains(((Dictionary<string, string>) emailPart)["Email"]), Is.True);
Assert.That(mimeMessage.TextBody!.Contains(text.Replace("{Email}", emailPart["Email"]).Replace("{Continent}", continentPart["Continent"])), Is.True);
});
MailMergeMessage.DisposeFileStreams(mimeMessage);
i++;
}
}
[Test]
public void SingleMessageFromValueTuple()
{
var anonymous = new { Email = "test@example.com" };
const string text = "Lorem ipsum dolor. Email={Email}, Continent={Continent}.";
var so = (anonymous, new Dictionary<string, string> { { "Continent", "Europe" } });
using var mmm = new MailMergeMessage("Subject for {Continent}", text);
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "from@example.com"));
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "{Email}"));
var mimeMessage = mmm.GetMimeMessage(so);
var (_, continentPart) = so;
Assert.Multiple(() =>
{
Assert.That(mimeMessage.To.ToString().Contains(anonymous.Email), Is.True);
Assert.That(mimeMessage.TextBody!.Contains(text.Replace("{Email}", anonymous.Email).Replace("{Continent}", continentPart["Continent"])), Is.True);
});
MailMergeMessage.DisposeFileStreams(mimeMessage);
}
private class Recipient
{
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
}
[Test]
public void MessagesFromList()
{
var recipients = new List<Recipient>();
for (var i = 0; i < 10; i++)
{
recipients.Add(new Recipient() { Email = $"recipient-{i}@example.com", Name = $"Name of {i}" });
}
using var mmm = new MailMergeMessage("Get MimeMessages Test", string.Empty,
"<html><head></head><body>This is the plain text part for {Name} ({Email})</body></html>");
mmm.ConvertHtmlToPlainText();
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "from@example.com"));
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "{Name}", "{Email}"));
var cnt = 0;
foreach (var mimeMessage in mmm.GetMimeMessages<Recipient>(recipients))
{
Assert.Multiple(() =>
{
Assert.That(mimeMessage.TextBody ==
string.Format(
$"This is the plain text part for {recipients[cnt].Name} ({recipients[cnt].Email})"), Is.True);
Assert.That(mimeMessage.HtmlBody!.Contains(string.Format(
$"This is the plain text part for {recipients[cnt].Name} ({recipients[cnt].Email})")), Is.True);
Assert.That(mimeMessage.To.ToString().Contains(recipients[cnt].Name) &&
mimeMessage.To.ToString().Contains(recipients[cnt].Email), Is.True);
});
MailMergeMessage.DisposeFileStreams(mimeMessage);
cnt++;
// The message could be sent using the low-level API using a configured SmtpClient:
// new SmtpClient().Send(FormatOptions.Default, mimeMessage);
}
}
[Test]
public void MessagesFromJsonArray()
{
var recipients = JArray.Parse(@"
[
{
'Email': 'email.1@example.com',
'Name': 'John'
},
{
'Email': 'email.2@example.com',
'Name': 'Mary'
},
{
'Email': 'email.3@example.com',
'Name': 'Steve'
}
]
");
using var mmm = new MailMergeMessage("Get MimeMessages JSON Test", string.Empty, "<html><head></head><body>This is the plain text part for {Name} ({Email})</body></html>");
mmm.ConvertHtmlToPlainText();
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "from@example.com"));
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "{Name}", "{Email}"));
var cnt = 0;
foreach (var mimeMessage in mmm.GetMimeMessages(recipients))
{
Assert.Multiple(() =>
{
Assert.That(mimeMessage.TextBody == string.Format($"This is the plain text part for {recipients[cnt]["Name"]} ({recipients[cnt]["Email"]})"), Is.True);
Assert.That(mimeMessage.HtmlBody!.Contains(string.Format($"This is the plain text part for {recipients[cnt]["Name"]} ({recipients[cnt]["Email"]})")), Is.True);
Assert.That(mimeMessage.To.ToString().Contains(recipients[cnt]["Name"]!.ToString()) && mimeMessage.To.ToString().Contains(recipients[cnt]["Email"]!.ToString()), Is.True);
});
MailMergeMessage.DisposeFileStreams(mimeMessage);
cnt++;
}
}
[Test]
public void Disabled_Formatter_Should_Maintain_Variable_Placeholders()
{
var anonymous = new { Email = "test@example.com" };
const string text = "Lorem ipsum dolor. Email={Email}, Continent={Continent}.";
var so = (anonymous, new Dictionary<string, string> { { "Continent", "Europe" } });
using var mmm = new MailMergeMessage("Subject for {Continent}", text)
{
EnableFormatter = false
};
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "from@example.com"));
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "{Name}","to@example.com"));
var mimeMessage = mmm.GetMimeMessage(so);
var (_, continentPart) = so;
Assert.Multiple(() =>
{
Assert.That(mimeMessage.Subject!.Contains("Subject for {Continent}"), Is.True);
Assert.That(mimeMessage.To.ToString().Contains("{Name}"), Is.True);
Assert.That(mimeMessage.TextBody!.Contains(text), Is.True);
});
MailMergeMessage.DisposeFileStreams(mimeMessage);
}
}