22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
44using System ;
5- using System . Net ;
6- using System . Net . Mail ;
5+ using System . Threading . Tasks ;
76using Microsoft . Extensions . Logging ;
8- using NuGet . Jobs ;
7+ using NuGet . Services . Messaging . Email ;
98
109namespace NuGet . SupportRequests . Notifications . Services
1110{
1211 internal class MessagingService
1312 {
14- private readonly MailAddress _fromAddress ;
1513 private readonly ILogger < MessagingService > _logger ;
16- private readonly string _smtpUri ;
17- private SmtpClient _smtpClient ;
14+ private readonly IMessageService _messageService ;
1815
19- private const string _noreplyAddress = "NuGet Gallery <[email protected] >" ; 20-
21- public MessagingService ( ILoggerFactory loggerFactory , string smtpUri )
16+ public MessagingService ( IMessageService messageService , ILogger < MessagingService > logger )
2217 {
23- if ( loggerFactory == null )
24- {
25- throw new ArgumentNullException ( nameof ( loggerFactory ) ) ;
26- }
27-
28- if ( smtpUri == null )
29- {
30- throw new ArgumentNullException ( nameof ( smtpUri ) ) ;
31- }
32-
33- _logger = loggerFactory . CreateLogger < MessagingService > ( ) ;
34- _fromAddress = new MailAddress ( _noreplyAddress ) ;
35- _smtpUri = smtpUri ;
18+ _messageService = messageService ?? throw new ArgumentNullException ( nameof ( messageService ) ) ;
19+ _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
3620 }
3721
38- internal void SendNotification (
22+ internal async Task SendNotification (
3923 string subject ,
40- string body ,
24+ string htmlBody ,
4125 DateTime referenceTime ,
4226 string targetEmailAddress )
4327 {
@@ -46,67 +30,23 @@ internal void SendNotification(
4630 throw new ArgumentException ( nameof ( subject ) ) ;
4731 }
4832
49- if ( string . IsNullOrEmpty ( body ) )
33+ if ( string . IsNullOrEmpty ( htmlBody ) )
5034 {
51- throw new ArgumentException ( nameof ( body ) ) ;
35+ throw new ArgumentException ( nameof ( htmlBody ) ) ;
5236 }
5337
5438 if ( string . IsNullOrEmpty ( targetEmailAddress ) )
5539 {
5640 throw new ArgumentException ( nameof ( targetEmailAddress ) ) ;
5741 }
5842
59- var targetAddress = new MailAddress ( targetEmailAddress ) ;
60- using ( var mailMessage = new MailMessage ( ) )
61- {
62- mailMessage . Subject = subject ;
63- mailMessage . From = _fromAddress ;
64- mailMessage . ReplyToList . Add ( _fromAddress ) ;
65- mailMessage . Body = body ;
66- mailMessage . To . Add ( targetAddress ) ;
67-
68- SendMessage ( mailMessage ) ;
69- }
43+ var builder = new SupportRequestNotificationEmailBuilder ( subject , htmlBody , targetEmailAddress ) ;
44+ await _messageService . SendMessageAsync ( builder ) ;
7045
7146 _logger . LogInformation (
7247 "Successfully sent notification '{NotificationType}' for reference time '{ReferenceTimeUtc}'" ,
7348 subject ,
7449 referenceTime . ToShortDateString ( ) ) ;
7550 }
76-
77- private void SendMessage ( MailMessage mailMessage )
78- {
79- var smtpClient = GetOrCreateSmtpClient ( ) ;
80-
81- var alternateHtmlView = AlternateView . CreateAlternateViewFromString ( mailMessage . Body , null , "text/html" ) ;
82- mailMessage . AlternateViews . Add ( alternateHtmlView ) ;
83-
84- smtpClient . Send ( mailMessage ) ;
85- }
86-
87- private SmtpClient GetOrCreateSmtpClient ( )
88- {
89- if ( _smtpClient != null )
90- {
91- return _smtpClient ;
92- }
93-
94- var smtpUri = new SmtpUri ( new Uri ( _smtpUri ) ) ;
95- _smtpClient = new SmtpClient ( ) ;
96- _smtpClient . Host = smtpUri . Host ;
97- _smtpClient . Port = smtpUri . Port ;
98- _smtpClient . DeliveryMethod = SmtpDeliveryMethod . Network ;
99- _smtpClient . EnableSsl = smtpUri . Secure ;
100-
101- if ( ! string . IsNullOrEmpty ( smtpUri . UserName ) )
102- {
103- _smtpClient . UseDefaultCredentials = false ;
104- _smtpClient . Credentials = new NetworkCredential (
105- smtpUri . UserName ,
106- smtpUri . Password ) ;
107- }
108-
109- return _smtpClient ;
110- }
11151 }
11252}
0 commit comments