1
1
//
2
- // Copyright (c) 2004-2016 Jaroslaw Kowalski <[email protected] >, Kim Christensen, Julian Verdurmen
2
+ // Copyright (c) 2004-2021 Jaroslaw Kowalski <[email protected] >, Kim Christensen, Julian Verdurmen
3
3
//
4
4
// All rights reserved.
5
5
//
31
31
// THE POSSIBILITY OF SUCH DAMAGE.
32
32
//
33
33
34
-
35
34
using System ;
36
35
using System . Collections . Generic ;
37
36
using System . ComponentModel ;
42
41
using MimeKit . Text ;
43
42
using NLog . Common ;
44
43
using NLog . Config ;
45
- using NLog . Internal ;
46
44
using NLog . Layouts ;
45
+ using NLog . MailKit . Util ;
47
46
using NLog . Targets ;
48
47
49
48
namespace NLog . MailKit
@@ -54,7 +53,7 @@ namespace NLog.MailKit
54
53
/// <seealso href="https://github.com/nlog/nlog/wiki/Mail-target">Documentation on NLog Wiki</seealso>
55
54
/// <example>
56
55
/// <p>
57
- /// To set up the target in the <a href="config.html">configuration file</a>,
56
+ /// To set up the target in the <a href="config.html">configuration file</a>,
58
57
/// use the following syntax:
59
58
/// </p>
60
59
/// <code lang="XML" source="examples/targets/Configuration File/Mail/Simple/NLog.config" />
@@ -71,7 +70,7 @@ namespace NLog.MailKit
71
70
/// which lets you send multiple log messages in single mail
72
71
/// </p>
73
72
/// <p>
74
- /// To set up the buffered mail target in the <a href="config.html">configuration file</a>,
73
+ /// To set up the buffered mail target in the <a href="config.html">configuration file</a>,
75
74
/// use the following syntax:
76
75
/// </p>
77
76
/// <code lang="XML" source="examples/targets/Configuration File/Mail/Buffered/NLog.config" />
@@ -91,7 +90,6 @@ public class MailTarget : TargetWithLayoutHeaderAndFooter
91
90
/// <remarks>
92
91
/// The default value of the layout is: <code>${longdate}|${level:uppercase=true}|${logger}|${message}</code>
93
92
/// </remarks>
94
- [ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Microsoft.Usage" , "CA2214:DoNotCallOverridableMethodsInConstructors" , Justification = "This one is safe." ) ]
95
93
public MailTarget ( )
96
94
{
97
95
Body = "${message}${newline}" ;
@@ -209,18 +207,20 @@ public Layout Body
209
207
/// <summary>
210
208
/// Gets or sets a value indicating whether SSL (secure sockets layer) should be used when communicating with SMTP server.
211
209
///
212
- /// See also <see cref="SecureSocketOption"/>
210
+ /// See also <see cref="SecureSocketOption" />
213
211
/// </summary>
214
- /// <docgen category='SMTP Options' order='14' />.
212
+ /// <docgen category='SMTP Options' order='14' />
213
+ /// .
215
214
[ DefaultValue ( false ) ]
216
215
public bool EnableSsl { get ; set ; }
217
216
218
217
/// <summary>
219
- /// Provides a way of specifying the SSL and/or TLS encryption
218
+ /// Provides a way of specifying the SSL and/or TLS encryption
220
219
///
221
- /// If <see cref="EnableSsl"/> is <c>true</c>, then <see cref="SecureSocketOptions.SslOnConnect"/> will be used.
220
+ /// If <see cref="EnableSsl" /> is <c>true</c>, then <see cref="SecureSocketOptions.SslOnConnect" /> will be used.
222
221
/// </summary>
223
222
[ DefaultValue ( SecureSocketOptions . StartTlsWhenAvailable ) ]
223
+ [ CLSCompliant ( false ) ]
224
224
public SecureSocketOptions SecureSocketOption { get ; set ; }
225
225
226
226
/// <summary>
@@ -233,7 +233,8 @@ public Layout Body
233
233
/// <summary>
234
234
/// Gets or sets a value indicating whether SmtpClient should ignore invalid certificate.
235
235
/// </summary>
236
- /// <docgen category='SMTP Options' order='16' />.
236
+ /// <docgen category='SMTP Options' order='16' />
237
+ /// .
237
238
[ DefaultValue ( false ) ]
238
239
public bool SkipCertificateValidation { get ; set ; }
239
240
@@ -243,9 +244,9 @@ public Layout Body
243
244
public Layout Priority { get ; set ; }
244
245
245
246
/// <summary>
246
- /// Gets or sets a value indicating whether NewLine characters in the body should be replaced with <br/> tags.
247
+ /// Gets or sets a value indicating whether NewLine characters in the body should be replaced with <br /> tags.
247
248
/// </summary>
248
- /// <remarks>Only happens when <see cref="Html"/> is set to true.</remarks>
249
+ /// <remarks>Only happens when <see cref="Html" /> is set to true.</remarks>
249
250
[ DefaultValue ( false ) ]
250
251
public bool ReplaceNewlineWithBrTagInHtml { get ; set ; }
251
252
@@ -265,24 +266,6 @@ protected override void Write(AsyncLogEventInfo logEvent)
265
266
Write ( ( IList < AsyncLogEventInfo > ) new [ ] { logEvent } ) ;
266
267
}
267
268
268
- #if ! NETSTANDARD2_0
269
-
270
- /// <summary>
271
- /// NOTE! Will soon be marked obsolete. Instead override Write(IList{AsyncLogEventInfo} logEvents)
272
- ///
273
- /// Writes an array of logging events to the log target. By default it iterates on all
274
- /// events and passes them to "Write" method. Inheriting classes can use this method to
275
- /// optimize batch writes.
276
- /// </summary>
277
- /// <param name="logEvents">Logging events to be written out.</param>
278
- [ Obsolete ( "Write(AsyncLogEventInfo[]) is obsolete. Override Write(IList{AsyncLogEventInfo} logEvents) instead." , true ) ]
279
- protected override void Write ( AsyncLogEventInfo [ ] logEvents )
280
- {
281
- Write ( ( IList < AsyncLogEventInfo > ) logEvents ) ;
282
- }
283
-
284
- #endif
285
-
286
269
/// <summary>
287
270
/// Renders an array logging events.
288
271
/// </summary>
@@ -306,16 +289,14 @@ protected override void InitializeTarget()
306
289
InternalLogger . Debug ( "Init mailtarget with mailkit" ) ;
307
290
CheckRequiredParameters ( ) ;
308
291
309
- if ( this . SmtpAuthentication == SmtpAuthenticationMode . Ntlm )
292
+ if ( SmtpAuthentication == SmtpAuthenticationMode . Ntlm )
310
293
{
311
294
throw new NLogConfigurationException ( "Ntlm not yet supported" ) ;
312
295
}
313
296
314
297
base . InitializeTarget ( ) ;
315
298
}
316
299
317
-
318
-
319
300
/// <summary>
320
301
/// Create mail and send with SMTP
321
302
/// </summary>
@@ -329,8 +310,8 @@ private void ProcessSingleMailMessage(IList<AsyncLogEventInfo> events)
329
310
throw new NLogRuntimeException ( "We need at least one event." ) ;
330
311
}
331
312
332
- LogEventInfo firstEvent = events [ 0 ] . LogEvent ;
333
- LogEventInfo lastEvent = events [ events . Count - 1 ] . LogEvent ;
313
+ var firstEvent = events [ 0 ] . LogEvent ;
314
+ var lastEvent = events [ events . Count - 1 ] . LogEvent ;
334
315
335
316
// unbuffered case, create a local buffer, append header, body and footer
336
317
var bodyBuffer = CreateBodyBuffer ( events , firstEvent , lastEvent ) ;
@@ -351,10 +332,12 @@ private void ProcessSingleMailMessage(IList<AsyncLogEventInfo> events)
351
332
var secureSocketOptions = EnableSsl ? SecureSocketOptions . SslOnConnect : SecureSocketOption ;
352
333
InternalLogger . Debug ( "Sending mail to {0} using {1}:{2} (socket option={3})" , message . To , renderedHost , SmtpPort , secureSocketOptions ) ;
353
334
InternalLogger . Trace ( " Subject: '{0}'" , message . Subject ) ;
354
- InternalLogger . Trace ( " From: '{0}'" , message . From . ToString ( ) ) ;
335
+ InternalLogger . Trace ( " From: '{0}'" , message . From ) ;
355
336
356
337
if ( SkipCertificateValidation )
338
+ {
357
339
client . ServerCertificateValidationCallback += ( s , cert , chain , sslPolicyErrors ) => true ;
340
+ }
358
341
359
342
360
343
client . Connect ( renderedHost , SmtpPort , secureSocketOptions ) ;
@@ -366,7 +349,7 @@ private void ProcessSingleMailMessage(IList<AsyncLogEventInfo> events)
366
349
367
350
// Note: only needed if the SMTP server requires authentication
368
351
369
- if ( this . SmtpAuthentication == SmtpAuthenticationMode . Basic )
352
+ if ( SmtpAuthentication == SmtpAuthenticationMode . Basic )
370
353
{
371
354
var userName = SmtpUserName ? . Render ( lastEvent ) ;
372
355
var password = SmtpPassword ? . Render ( lastEvent ) ;
@@ -423,7 +406,7 @@ private StringBuilder CreateBodyBuffer(IEnumerable<AsyncLogEventInfo> events, Lo
423
406
}
424
407
}
425
408
426
- foreach ( AsyncLogEventInfo eventInfo in events )
409
+ foreach ( var eventInfo in events )
427
410
{
428
411
bodyBuffer . Append ( Layout . Render ( eventInfo . LogEvent ) ) ;
429
412
if ( AddNewLines )
@@ -440,6 +423,7 @@ private StringBuilder CreateBodyBuffer(IEnumerable<AsyncLogEventInfo> events, Lo
440
423
bodyBuffer . Append ( "\n " ) ;
441
424
}
442
425
}
426
+
443
427
return bodyBuffer ;
444
428
}
445
429
@@ -459,8 +443,8 @@ private void CheckRequiredParameters()
459
443
/// <summary>
460
444
/// Create key for grouping. Needed for multiple events in one mailmessage
461
445
/// </summary>
462
- /// <param name="logEvent">event for rendering layouts </param>
463
- ///<returns>string to group on</returns>
446
+ /// <param name="logEvent">event for rendering layouts </param>
447
+ /// <returns>string to group on</returns>
464
448
private string GetSmtpSettingsKey ( LogEventInfo logEvent )
465
449
{
466
450
var sb = new StringBuilder ( ) ;
@@ -472,26 +456,25 @@ private string GetSmtpSettingsKey(LogEventInfo logEvent)
472
456
AppendLayout ( sb , logEvent , SmtpServer ) ;
473
457
AppendLayout ( sb , logEvent , SmtpPassword ) ;
474
458
AppendLayout ( sb , logEvent , SmtpUserName ) ;
475
-
476
-
459
+
477
460
return sb . ToString ( ) ;
478
461
}
479
462
480
463
/// <summary>
481
464
/// Append rendered layout to the stringbuilder
482
465
/// </summary>
483
466
/// <param name="sb">append to this</param>
484
- /// <param name="logEvent">event for rendering <paramref name="layout"/></param>
467
+ /// <param name="logEvent">event for rendering <paramref name="layout" /></param>
485
468
/// <param name="layout">append if not <c>null</c></param>
486
469
private static void AppendLayout ( StringBuilder sb , LogEventInfo logEvent , Layout layout )
487
470
{
488
471
sb . Append ( "|" ) ;
489
472
if ( layout != null )
473
+ {
490
474
sb . Append ( layout . Render ( logEvent ) ) ;
475
+ }
491
476
}
492
477
493
-
494
-
495
478
/// <summary>
496
479
/// Create the mailmessage with the addresses, properties and body.
497
480
/// </summary>
@@ -505,6 +488,7 @@ private MimeMessage CreateMailMessage(LogEventInfo lastEvent, string body)
505
488
{
506
489
throw new NLogRuntimeException ( RequiredPropertyIsEmptyFormat , "From" ) ;
507
490
}
491
+
508
492
msg . From . Add ( MailboxAddress . Parse ( renderedFrom ) ) ;
509
493
510
494
var addedTo = AddAddresses ( msg . To , To , lastEvent ) ;
@@ -523,22 +507,21 @@ private MimeMessage CreateMailMessage(LogEventInfo lastEvent, string body)
523
507
if ( Priority != null )
524
508
{
525
509
var renderedPriority = Priority . Render ( lastEvent ) ;
526
- MessagePriority messagePriority ;
527
- messagePriority = ParseMessagePriority ( renderedPriority ) ;
528
- msg . Priority = messagePriority ;
510
+ msg . Priority = ParseMessagePriority ( renderedPriority ) ;
529
511
}
530
512
531
513
TextPart CreateBodyPart ( )
532
514
{
533
515
var newBody = body ;
534
516
if ( Html && ReplaceNewlineWithBrTagInHtml )
517
+ {
535
518
newBody = newBody ? . Replace ( Environment . NewLine , "<br/>" ) ;
519
+ }
520
+
536
521
return new TextPart ( Html ? TextFormat . Html : TextFormat . Plain )
537
522
{
538
523
Text = newBody ,
539
524
ContentType = { Charset = Encoding ? . WebName }
540
-
541
-
542
525
} ;
543
526
}
544
527
@@ -547,7 +530,7 @@ TextPart CreateBodyPart()
547
530
return msg ;
548
531
}
549
532
550
- public static MessagePriority ParseMessagePriority ( string priority )
533
+ internal static MessagePriority ParseMessagePriority ( string priority )
551
534
{
552
535
if ( string . IsNullOrWhiteSpace ( priority ) )
553
536
{
@@ -559,10 +542,12 @@ public static MessagePriority ParseMessagePriority(string priority)
559
542
{
560
543
return MessagePriority . Urgent ;
561
544
}
545
+
562
546
if ( priority . Equals ( "Low" , StringComparison . OrdinalIgnoreCase ) )
563
547
{
564
548
return MessagePriority . NonUrgent ;
565
549
}
550
+
566
551
MessagePriority messagePriority ;
567
552
try
568
553
{
@@ -580,26 +565,27 @@ public static MessagePriority ParseMessagePriority(string priority)
580
565
}
581
566
582
567
/// <summary>
583
- /// Render <paramref name="layout"/> and add the addresses to <paramref name="mailAddressCollection"/>
568
+ /// Render <paramref name="layout" /> and add the addresses to <paramref name="mailAddressCollection" />
584
569
/// </summary>
585
570
/// <param name="mailAddressCollection">Addresses appended to this list</param>
586
571
/// <param name="layout">layout with addresses, ; separated</param>
587
- /// <param name="logEvent">event for rendering the <paramref name="layout"/></param>
572
+ /// <param name="logEvent">event for rendering the <paramref name="layout" /></param>
588
573
/// <returns>added a address?</returns>
589
574
private static bool AddAddresses ( InternetAddressList mailAddressCollection , Layout layout , LogEventInfo logEvent )
590
575
{
576
+ if ( layout == null )
577
+ {
578
+ return false ;
579
+ }
580
+
591
581
var added = false ;
592
- if ( layout != null )
582
+ foreach ( var mail in layout . Render ( logEvent ) . Split ( new [ ] { ';' } , StringSplitOptions . RemoveEmptyEntries ) )
593
583
{
594
- foreach ( string mail in layout . Render ( logEvent ) . Split ( new [ ] { ';' } , StringSplitOptions . RemoveEmptyEntries ) )
595
- {
596
- mailAddressCollection . Add ( MailboxAddress . Parse ( mail ) ) ;
597
- added = true ;
598
- }
584
+ mailAddressCollection . Add ( MailboxAddress . Parse ( mail ) ) ;
585
+ added = true ;
599
586
}
600
587
601
588
return added ;
602
589
}
603
590
}
604
- }
605
-
591
+ }
0 commit comments