This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Integrate NuGet.SupportRequests.Notifications with NuGet.Services.Messaging.Email #606
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,42 +2,26 @@ | |
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Net; | ||
using System.Net.Mail; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using NuGet.Jobs; | ||
using NuGet.Services.Messaging.Email; | ||
|
||
namespace NuGet.SupportRequests.Notifications.Services | ||
{ | ||
internal class MessagingService | ||
{ | ||
private readonly MailAddress _fromAddress; | ||
private readonly ILogger<MessagingService> _logger; | ||
private readonly string _smtpUri; | ||
private SmtpClient _smtpClient; | ||
private readonly IMessageService _messageService; | ||
|
||
private const string _noreplyAddress = "NuGet Gallery <[email protected]>"; | ||
|
||
public MessagingService(ILoggerFactory loggerFactory, string smtpUri) | ||
public MessagingService(IMessageService messageService, ILogger<MessagingService> logger) | ||
{ | ||
if (loggerFactory == null) | ||
{ | ||
throw new ArgumentNullException(nameof(loggerFactory)); | ||
} | ||
|
||
if (smtpUri == null) | ||
{ | ||
throw new ArgumentNullException(nameof(smtpUri)); | ||
} | ||
|
||
_logger = loggerFactory.CreateLogger<MessagingService>(); | ||
_fromAddress = new MailAddress(_noreplyAddress); | ||
_smtpUri = smtpUri; | ||
_messageService = messageService ?? throw new ArgumentNullException(nameof(messageService)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
internal void SendNotification( | ||
internal async Task SendNotification( | ||
string subject, | ||
string body, | ||
string htmlBody, | ||
DateTime referenceTime, | ||
string targetEmailAddress) | ||
{ | ||
|
@@ -46,67 +30,23 @@ internal void SendNotification( | |
throw new ArgumentException(nameof(subject)); | ||
} | ||
|
||
if (string.IsNullOrEmpty(body)) | ||
if (string.IsNullOrEmpty(htmlBody)) | ||
{ | ||
throw new ArgumentException(nameof(body)); | ||
throw new ArgumentException(nameof(htmlBody)); | ||
} | ||
|
||
if (string.IsNullOrEmpty(targetEmailAddress)) | ||
{ | ||
throw new ArgumentException(nameof(targetEmailAddress)); | ||
} | ||
|
||
var targetAddress = new MailAddress(targetEmailAddress); | ||
using (var mailMessage = new MailMessage()) | ||
{ | ||
mailMessage.Subject = subject; | ||
mailMessage.From = _fromAddress; | ||
mailMessage.ReplyToList.Add(_fromAddress); | ||
mailMessage.Body = body; | ||
mailMessage.To.Add(targetAddress); | ||
|
||
SendMessage(mailMessage); | ||
} | ||
var builder = new SupportRequestNotificationEmailBuilder(subject, htmlBody, targetEmailAddress); | ||
await _messageService.SendMessageAsync(builder); | ||
|
||
_logger.LogInformation( | ||
"Successfully sent notification '{NotificationType}' for reference time '{ReferenceTimeUtc}'", | ||
subject, | ||
referenceTime.ToShortDateString()); | ||
} | ||
|
||
private void SendMessage(MailMessage mailMessage) | ||
{ | ||
var smtpClient = GetOrCreateSmtpClient(); | ||
|
||
var alternateHtmlView = AlternateView.CreateAlternateViewFromString(mailMessage.Body, null, "text/html"); | ||
mailMessage.AlternateViews.Add(alternateHtmlView); | ||
|
||
smtpClient.Send(mailMessage); | ||
} | ||
|
||
private SmtpClient GetOrCreateSmtpClient() | ||
{ | ||
if (_smtpClient != null) | ||
{ | ||
return _smtpClient; | ||
} | ||
|
||
var smtpUri = new SmtpUri(new Uri(_smtpUri)); | ||
_smtpClient = new SmtpClient(); | ||
_smtpClient.Host = smtpUri.Host; | ||
_smtpClient.Port = smtpUri.Port; | ||
_smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; | ||
_smtpClient.EnableSsl = smtpUri.Secure; | ||
|
||
if (!string.IsNullOrEmpty(smtpUri.UserName)) | ||
{ | ||
_smtpClient.UseDefaultCredentials = false; | ||
_smtpClient.Credentials = new NetworkCredential( | ||
smtpUri.UserName, | ||
smtpUri.Password); | ||
} | ||
|
||
return _smtpClient; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/NuGet.SupportRequests.Notifications/SupportRequestNotificationEmailBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Net.Mail; | ||
using NuGet.Services.Messaging.Email; | ||
|
||
namespace NuGet.SupportRequests.Notifications | ||
{ | ||
public class SupportRequestNotificationEmailBuilder : IEmailBuilder | ||
{ | ||
public SupportRequestNotificationEmailBuilder( | ||
string subject, | ||
string htmlBody, | ||
string targetEmailAddress) | ||
{ | ||
_subject = subject; | ||
_htmlBody = htmlBody; | ||
_targetAddress = new MailAddress(targetEmailAddress); | ||
} | ||
|
||
public static MailAddress NoReplyAddress = new MailAddress("NuGet Gallery <[email protected]>"); | ||
public MailAddress Sender => NoReplyAddress; | ||
|
||
private MailAddress _targetAddress; | ||
public IEmailRecipients GetRecipients() | ||
{ | ||
return new EmailRecipients(to: new[] { _targetAddress }); | ||
} | ||
|
||
private readonly string _subject; | ||
public string GetSubject() | ||
{ | ||
return _subject; | ||
} | ||
|
||
private readonly string _htmlBody; | ||
public string GetBody(EmailFormat format) | ||
{ | ||
// We only create an HTML version of the body for now. | ||
// Ideally we would create a plaintext fallback as well, and return a different version of the body based on the format. | ||
// For now, however, returning an HTML body as the plaintext fallback is better than having a completely empty plaintext fallback. | ||
return _htmlBody; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Null checks?