Skip to content

Commit 537e490

Browse files
authored
Merge pull request #4 from MattMofDoom/dev
v1.1.9
2 parents f656161 + 39935aa commit 537e490

13 files changed

Lines changed: 269 additions & 151 deletions

Lurgle.Alerting.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Aaaah/@EntryIndexedValue">True</s:Boolean>
23
<s:Boolean x:Key="/Default/UserDictionary/Words/=Lurgle/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

Lurgle.Alerting/Alert.cs

Lines changed: 45 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public sealed class Alert : IAlert, IEnvelope
3636
private AlertLevel priority = AlertLevel.Normal;
3737
private Address replyTo = new Address();
3838
private string subject = string.Empty;
39+
40+
// ReSharper disable once FieldCanBeMadeReadOnly.Local
3941
private List<Address> to = new List<Address>();
4042

4143
private Alert()
@@ -44,7 +46,6 @@ private Alert()
4446
}
4547

4648
private bool IsMethod { get; set; }
47-
private bool IsDebug { get; set; }
4849
private string MethodName { get; set; }
4950

5051
/// <summary>
@@ -70,16 +71,10 @@ private Alert()
7071
public IEnvelope To(string toAddress, string toName = null, AddressType addressType = AddressType.Email)
7172
{
7273
var emailAddress = string.IsNullOrEmpty(toAddress)
73-
? GetEmailAddress(Alerting.Config.MailTo, AddressType.Email, IsDebug)
74-
: GetEmailAddress(toAddress, addressType, IsDebug);
75-
76-
{
77-
var emailList = string.Join(",", emailAddress.Split(';')).Split(',');
74+
? Alerting.GetEmailAddress(Alerting.Config.MailTo, addressType)
75+
: Alerting.GetEmailAddress(toAddress, addressType);
7876

79-
if (emailList.Length > 1)
80-
return To(emailList);
81-
to.Add(new Address(emailAddress, toName));
82-
}
77+
to.AddRange(ToAddressList(emailAddress, toName));
8378

8479
return this;
8580
}
@@ -95,7 +90,7 @@ public IEnvelope To(IEnumerable<string> emailList)
9590
{
9691
foreach (var toAddress in emailList)
9792
if (!string.IsNullOrEmpty(toAddress))
98-
to.Add(new Address(GetEmailAddress(toAddress, AddressType.Email, IsDebug)));
93+
to.AddRange(ToAddressList(Alerting.GetEmailAddress(toAddress)));
9994

10095
return this;
10196
}
@@ -112,8 +107,8 @@ public IEnvelope To(Dictionary<string, string> emailList)
112107
foreach (var email in emailList)
113108
if (!string.IsNullOrEmpty(email.Key))
114109
{
115-
var emailAddress = GetEmailAddress(email.Key, AddressType.Email, IsDebug);
116-
if (!string.IsNullOrEmpty(emailAddress)) to.Add(new Address(emailAddress, email.Value));
110+
var emailAddress = Alerting.GetEmailAddress(email.Key);
111+
if (!string.IsNullOrEmpty(emailAddress)) to.AddRange(ToAddressList(emailAddress, email.Value));
117112
}
118113

119114
return this;
@@ -139,15 +134,8 @@ public IEnvelope To(Dictionary<string, string> emailList)
139134
/// <returns></returns>
140135
public IEnvelope Cc(string ccAddress, string ccName = null, AddressType addressType = AddressType.Email)
141136
{
142-
if (!string.IsNullOrEmpty(ccAddress))
143-
{
144-
var emailList = string.Join(",", ccAddress.Split(';')).Split(',');
145-
146-
if (emailList.Length > 1) return Cc(emailList);
147-
}
148-
149-
var emailAddress = GetEmailAddress(ccAddress, addressType, IsDebug);
150-
if (!string.IsNullOrEmpty(emailAddress)) cc.Add(new Address(emailAddress, ccName));
137+
var emailAddress = Alerting.GetEmailAddress(ccAddress, addressType);
138+
if (!string.IsNullOrEmpty(emailAddress)) cc.AddRange(ToAddressList(emailAddress, ccName));
151139

152140
return this;
153141
}
@@ -163,8 +151,8 @@ public IEnvelope Cc(IEnumerable<string> emailList)
163151
{
164152
foreach (var ccAddress in emailList)
165153
{
166-
var emailAddress = GetEmailAddress(ccAddress, AddressType.Email, IsDebug);
167-
if (!string.IsNullOrEmpty(emailAddress)) cc.Add(new Address(emailAddress));
154+
var emailAddress = Alerting.GetEmailAddress(ccAddress);
155+
if (!string.IsNullOrEmpty(emailAddress)) cc.AddRange(ToAddressList(emailAddress));
168156
}
169157

170158
return this;
@@ -181,8 +169,8 @@ public IEnvelope Cc(Dictionary<string, string> emailList)
181169
{
182170
foreach (var email in emailList)
183171
{
184-
var emailAddress = GetEmailAddress(email.Key, AddressType.Email, IsDebug);
185-
if (!string.IsNullOrEmpty(emailAddress)) cc.Add(new Address(emailAddress, email.Value));
172+
var emailAddress = Alerting.GetEmailAddress(email.Key);
173+
if (!string.IsNullOrEmpty(emailAddress)) cc.AddRange(ToAddressList(emailAddress, email.Value));
186174
}
187175

188176
return this;
@@ -208,15 +196,8 @@ public IEnvelope Cc(Dictionary<string, string> emailList)
208196
/// <returns></returns>
209197
public IEnvelope Bcc(string bccAddress, string bccName = null, AddressType addressType = AddressType.Email)
210198
{
211-
if (!string.IsNullOrEmpty(bccAddress))
212-
{
213-
var emailList = string.Join(",", bccAddress.Split(';')).Split(',');
214-
215-
if (emailList.Length > 1) return Bcc(emailList);
216-
}
217-
218-
var emailAddress = GetEmailAddress(bccAddress, addressType, IsDebug);
219-
if (!string.IsNullOrEmpty(emailAddress)) bcc.Add(new Address(emailAddress, bccName));
199+
var emailAddress = Alerting.GetEmailAddress(bccAddress, addressType);
200+
if (!string.IsNullOrEmpty(emailAddress)) bcc.AddRange(ToAddressList(emailAddress, bccName));
220201

221202
return this;
222203
}
@@ -232,8 +213,8 @@ public IEnvelope Bcc(IEnumerable<string> emailList)
232213
{
233214
foreach (var bccAddress in emailList)
234215
{
235-
var emailAddress = GetEmailAddress(bccAddress, AddressType.Email, IsDebug);
236-
if (!string.IsNullOrEmpty(emailAddress)) bcc.Add(new Address(emailAddress));
216+
var emailAddress = Alerting.GetEmailAddress(bccAddress);
217+
if (!string.IsNullOrEmpty(emailAddress)) bcc.AddRange(ToAddressList(emailAddress));
237218
}
238219

239220
return this;
@@ -249,9 +230,9 @@ public IEnvelope Bcc(IEnumerable<string> emailList)
249230
public IEnvelope Bcc(Dictionary<string, string> emailList)
250231
{
251232
foreach (var email in from email in emailList
252-
let emailAddress = GetEmailAddress(email.Key, AddressType.Email, IsDebug)
233+
let emailAddress = Alerting.GetEmailAddress(email.Key)
253234
where !string.IsNullOrEmpty(emailAddress)
254-
select email) bcc.Add(new Address(email.Key, email.Value));
235+
select email) bcc.AddRange(ToAddressList(email.Key, email.Value));
255236

256237
return this;
257238
}
@@ -272,10 +253,11 @@ public IEnvelope ReplyTo(string replyToAddress = null, string replyToName = null
272253
AddressType addressType = AddressType.Email)
273254
{
274255
var emailAddress = string.IsNullOrEmpty(replyToAddress)
275-
? GetEmailAddress(Alerting.Config.MailFrom, AddressType.Email, IsDebug)
276-
: GetEmailAddress(replyToAddress, addressType, IsDebug);
256+
? Alerting.GetEmailAddress(Alerting.Config.MailFrom, addressType)
257+
: Alerting.GetEmailAddress(replyToAddress, addressType);
277258

278-
if (!string.IsNullOrEmpty(emailAddress)) replyTo = new Address(emailAddress, replyToName);
259+
//We can only select one email address for replyTo, so pick the first
260+
if (!string.IsNullOrEmpty(emailAddress)) replyTo = ToAddressList(emailAddress, replyToName).First();
279261

280262
return this;
281263
}
@@ -380,14 +362,14 @@ public IEnvelope Attach(IEnumerable<string> fileList)
380362
}
381363

382364
/// <summary>
383-
/// Attach a file opened as a stream to the alert
365+
/// Attach a file opened as a stream to the alert
384366
/// </summary>
385367
/// <param name="fileStream"></param>
386368
/// <param name="fileName"></param>
387369
/// <returns></returns>
388370
public IEnvelope Attach(Stream fileStream, string fileName)
389371
{
390-
attachments.Add(new Attachment { Data = fileStream, Filename = fileName, ContentType = null});
372+
attachments.Add(new Attachment {Data = fileStream, Filename = fileName, ContentType = null});
391373

392374
return this;
393375
}
@@ -707,6 +689,19 @@ public async Task<bool> SendTemplateFileAsync<T>(string templateConfig, T templa
707689
return result.Successful;
708690
}
709691

692+
/// <summary>
693+
/// Return a list of valid email addresses
694+
/// </summary>
695+
/// <param name="emailValue"></param>
696+
/// <param name="toName"></param>
697+
/// <returns></returns>
698+
public static IEnumerable<Address> ToAddressList(string emailValue, string toName = null)
699+
{
700+
return (from emailAddress in string.Join(",", emailValue.Split(';')).Split(',').ToList()
701+
where Alerting.IsValidEmail(emailAddress)
702+
select new Address(emailAddress, toName)).ToList();
703+
}
704+
710705
/// <summary>
711706
/// Attach a list of files to the alert
712707
/// <para />
@@ -786,31 +781,6 @@ private static void GetInlineFile(string inlineFile, string folderLocation, out
786781
filePath = Path.Combine(folderLocation, filePath);
787782
}
788783

789-
/// <summary>
790-
/// Resolve email addresses using the given <see cref="AddressType" />
791-
/// <para />
792-
/// If isDebug is true, emails will automatically be replaced with the configured debug email address.
793-
/// <para />
794-
/// </summary>
795-
/// <param name="emailType">Email config item or email address</param>
796-
/// <param name="addressType">Type of email being passed</param>
797-
/// <param name="isDebug">If true, emails will be replaced with the default <see cref="AlertConfig.MailTo" /></param>
798-
/// <returns></returns>
799-
private static string GetEmailAddress(string emailType, AddressType addressType, bool isDebug)
800-
{
801-
//If we have an empty string, we won't be able to resolve this, so return an empty string
802-
if (string.IsNullOrEmpty(emailType)) return string.Empty;
803-
804-
var emailAddress = addressType.Equals(AddressType.FromConfig)
805-
? AlertConfig.GetEmailConfig(emailType)
806-
: emailType;
807-
808-
//Automatically substitute for a debug email address if the debug flag is set
809-
if (isDebug) emailAddress = AlertConfig.GetEmailConfig("emailDebug");
810-
811-
return emailAddress;
812-
}
813-
814784
/// <summary>
815785
/// Instantiate a new email with the desired From address.
816786
/// <para />
@@ -822,15 +792,12 @@ private static string GetEmailAddress(string emailType, AddressType addressType,
822792
/// Type of email address - defaults to <see cref="AddressType.Email" /> but accepts
823793
/// <see cref="AddressType.FromConfig" /> to read from config
824794
/// </param>
825-
/// <param name="isDebug">
826-
/// If set to True, To, Cc, Bcc, and ReplyTo addresses will be overridden with the default
827-
/// <see cref="AlertConfig.MailTo" /> email
828-
/// </param>
829795
/// <param name="isMethod">Add the calling method to the message text if using <see cref="Send" /> to send an email</param>
830796
/// <param name="methodName">Automatically captures the calling method via [CallerMemberName]</param>
831797
/// <returns></returns>
798+
// ReSharper disable once MemberCanBePrivate.Global
832799
public static IEnvelope From(string fromAddress = null, string fromName = null,
833-
AddressType addressType = AddressType.Email, bool isDebug = false, bool isMethod = false,
800+
AddressType addressType = AddressType.Email, bool isMethod = false,
834801
[CallerMemberName] string methodName = null)
835802
{
836803
if (Alerting.Config == null) Alerting.Init();
@@ -846,8 +813,8 @@ public static IEnvelope From(string fromAddress = null, string fromName = null,
846813

847814
return new Alert
848815
{
849-
from = new Address(emailAddress, fromName), MethodName = methodName, IsDebug = isDebug,
850-
IsMethod = isMethod
816+
//We can only select one email address for From, so pick the first
817+
from = ToAddressList(emailAddress, fromName).First(), MethodName = methodName, IsMethod = isMethod
851818
};
852819
}
853820

@@ -869,39 +836,15 @@ public static IEnvelope From(string fromAddress = null, string fromName = null,
869836
/// Type of email address - defaults to <see cref="AddressType.Email" /> but accepts
870837
/// <see cref="AddressType.FromConfig" /> to read from config
871838
/// </param>
872-
/// <param name="isDebug">
873-
/// If set to True, To, Cc, Bcc, and ReplyTo addresses will be overridden with the default
874-
/// <see cref="AlertConfig.MailTo" /> email
875-
/// </param>
876839
/// <param name="isMethod">Add the calling method to the message text if using <see cref="Send" /> to send an email</param>
877840
/// <param name="methodName">Automatically captures the calling method via [CallerMemberName]</param>
878841
/// <returns></returns>
879842
public static IEnvelope To(string toAddress = null, string toName = null,
880843
// ReSharper disable once MethodOverloadWithOptionalParameter
881-
AddressType addressType = AddressType.Email, bool isDebug = false, bool isMethod = false,
844+
AddressType addressType = AddressType.Email, bool isMethod = false,
882845
[CallerMemberName] string methodName = null)
883846
{
884-
if (Alerting.Config == null) Alerting.Init();
885-
886-
var emailAddress = string.IsNullOrEmpty(toAddress)
887-
? GetEmailAddress(Alerting.Config?.MailTo, AddressType.Email, isDebug)
888-
: GetEmailAddress(toAddress, addressType, isDebug);
889-
890-
var emailList = string.Join(",", emailAddress.Split(';')).Split(',');
891-
892-
var toList = new List<Address>();
893-
if (emailList.Length > 1)
894-
toList.AddRange(from toEmail in emailList
895-
where !string.IsNullOrEmpty(toEmail)
896-
select new Address(toEmail));
897-
else
898-
toList.Add(new Address(emailAddress, toName));
899-
900-
return new Alert
901-
{
902-
from = new Address(Alerting.Config?.MailFrom), to = toList, MethodName = methodName, IsDebug = isDebug,
903-
IsMethod = isMethod
904-
};
847+
return From(methodName: methodName, isMethod: isMethod).To(toAddress, toName, addressType);
905848
}
906849
}
907850
}

Lurgle.Alerting/AlertConfig.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ private AlertConfig()
4343
/// <param name="mailTimeout"></param>
4444
/// <param name="mailFrom"></param>
4545
/// <param name="mailTo"></param>
46+
/// <param name="mailDebug"></param>
4647
/// <param name="mailSubject"></param>
4748
public AlertConfig(AlertConfig config = null, string appName = null, string appVersion = null,
4849
RendererType? mailRenderer = null,
4950
SenderType? mailSender = null, string mailTemplatePath = null, string mailHost = null,
5051
int? mailPort = null, int? mailTestTimeout = null, bool? mailUseAuthentication = null,
5152
string mailUsername = null,
5253
string mailPassword = null, bool? mailUseTls = null, int? mailTimeout = null, string mailFrom = null,
53-
string mailTo = null, string mailSubject = null)
54+
string mailTo = null, string mailDebug = null, string mailSubject = null)
5455
{
5556
if (config != null)
5657
{
@@ -69,6 +70,7 @@ public AlertConfig(AlertConfig config = null, string appName = null, string appV
6970
MailTimeout = config.MailTimeout;
7071
MailFrom = config.MailFrom;
7172
MailTo = config.MailTo;
73+
MailDebug = config.MailDebug;
7274
MailSubject = config.MailSubject;
7375
}
7476

@@ -102,6 +104,8 @@ public AlertConfig(AlertConfig config = null, string appName = null, string appV
102104
MailFrom = mailFrom;
103105
if (!string.IsNullOrEmpty(mailTo))
104106
MailTo = mailTo;
107+
if (!string.IsNullOrEmpty(mailDebug))
108+
MailDebug = mailDebug;
105109
if (!string.IsNullOrEmpty(mailSubject))
106110
MailSubject = mailSubject;
107111

@@ -199,6 +203,11 @@ public AlertConfig(AlertConfig config = null, string appName = null, string appV
199203
/// </summary>
200204
public string MailTo { get; private set; }
201205

206+
/// <summary>
207+
/// Default recipient address to substitute if IsDebug is enabled.
208+
/// </summary>
209+
public string MailDebug { get; private set; }
210+
202211
/// <summary>
203212
/// Default subject for emails. Defaults to "Alert!"
204213
/// </summary>
@@ -229,6 +238,7 @@ public static AlertConfig GetConfig(AlertConfig config = null)
229238
MailTimeout = GetTimeout(ConfigurationManager.AppSettings["MailTimeout"]),
230239
MailFrom = ConfigurationManager.AppSettings["MailFrom"],
231240
MailTo = ConfigurationManager.AppSettings["MailTo"],
241+
MailDebug = ConfigurationManager.AppSettings["MailDebug"],
232242
MailSubject = ConfigurationManager.AppSettings["MailSubject"]
233243
};
234244
else

0 commit comments

Comments
 (0)