Skip to content

Commit b63a1a2

Browse files
Support System.Net.Mail.MailPriority values: high and low (#83)
1 parent 7d1d4ba commit b63a1a2

File tree

2 files changed

+69
-14
lines changed

2 files changed

+69
-14
lines changed

src/NLog.MailKit/MailTarget.cs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public Layout Body
236236
/// <docgen category='SMTP Options' order='16' />.
237237
[DefaultValue(false)]
238238
public bool SkipCertificateValidation { get; set; }
239-
239+
240240
/// <summary>
241241
/// Gets or sets the priority used for sending mails.
242242
/// </summary>
@@ -352,11 +352,11 @@ private void ProcessSingleMailMessage(IList<AsyncLogEventInfo> events)
352352
InternalLogger.Debug("Sending mail to {0} using {1}:{2} (socket option={3})", message.To, renderedHost, SmtpPort, secureSocketOptions);
353353
InternalLogger.Trace(" Subject: '{0}'", message.Subject);
354354
InternalLogger.Trace(" From: '{0}'", message.From.ToString());
355-
356-
if(SkipCertificateValidation)
355+
356+
if (SkipCertificateValidation)
357357
client.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
358358

359-
359+
360360
client.Connect(renderedHost, SmtpPort, secureSocketOptions);
361361
InternalLogger.Trace(" Connecting succesfull");
362362

@@ -523,16 +523,9 @@ private MimeMessage CreateMailMessage(LogEventInfo lastEvent, string body)
523523
if (Priority != null)
524524
{
525525
var renderedPriority = Priority.Render(lastEvent);
526-
try
527-
{
528-
529-
msg.Priority = (MessagePriority)Enum.Parse(typeof(MessagePriority), renderedPriority, true);
530-
}
531-
catch
532-
{
533-
InternalLogger.Warn("Could not convert '{0}' to MessagePriority, valid values are NonUrgent, Normal and Urgent. Using normal priority as fallback.");
534-
msg.Priority = MessagePriority.Normal;
535-
}
526+
MessagePriority messagePriority;
527+
messagePriority = ParseMessagePriority(renderedPriority);
528+
msg.Priority = messagePriority;
536529
}
537530

538531
TextPart CreateBodyPart()
@@ -554,6 +547,38 @@ TextPart CreateBodyPart()
554547
return msg;
555548
}
556549

550+
public static MessagePriority ParseMessagePriority(string priority)
551+
{
552+
if (string.IsNullOrWhiteSpace(priority))
553+
{
554+
return MessagePriority.Normal;
555+
}
556+
557+
priority = priority.Trim();
558+
if (priority.Equals("High", StringComparison.OrdinalIgnoreCase))
559+
{
560+
return MessagePriority.Urgent;
561+
}
562+
if (priority.Equals("Low", StringComparison.OrdinalIgnoreCase))
563+
{
564+
return MessagePriority.NonUrgent;
565+
}
566+
MessagePriority messagePriority;
567+
try
568+
{
569+
messagePriority = (MessagePriority)Enum.Parse(typeof(MessagePriority), priority, true);
570+
}
571+
catch
572+
{
573+
InternalLogger.Warn("Could not convert '{0}' to MessagePriority, valid values are NonUrgent, Normal and Urgent. " +
574+
"Also High and Low could be used as alternatives to Urgent and NonUrgent. " +
575+
"Using normal priority as fallback.");
576+
messagePriority = MessagePriority.Normal;
577+
}
578+
579+
return messagePriority;
580+
}
581+
557582
/// <summary>
558583
/// Render <paramref name="layout"/> and add the addresses to <paramref name="mailAddressCollection"/>
559584
/// </summary>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using NLog.MailKit;
2+
using System;
3+
using MimeKit;
4+
using Xunit;
5+
6+
namespace NLog.MailKit.Tests
7+
{
8+
public class MailTargetTests
9+
{
10+
[Theory]
11+
[InlineData("high", MessagePriority.Urgent)]
12+
[InlineData("HIGH", MessagePriority.Urgent)]
13+
[InlineData(" HIGH ", MessagePriority.Urgent)]
14+
[InlineData("low", MessagePriority.NonUrgent)]
15+
[InlineData("LOW", MessagePriority.NonUrgent)]
16+
[InlineData(" LOW ", MessagePriority.NonUrgent)]
17+
[InlineData("Urgent", MessagePriority.Urgent)]
18+
[InlineData("URGENT", MessagePriority.Urgent)]
19+
[InlineData(" URGENT ", MessagePriority.Urgent)]
20+
[InlineData("normal", MessagePriority.Normal)]
21+
public void ParseMessagePriorityTests(string input, MessagePriority expected)
22+
{
23+
// Act
24+
var result = MailTarget.ParseMessagePriority(input);
25+
26+
// Assert
27+
Assert.Equal(expected, result);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)