-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for SMTP OAuth authentication through easier IEmailSenderClient implementation #17484
Merged
bergmania
merged 10 commits into
umbraco:contrib
from
KXCPH:temp/email-oauth-and-custom
Nov 19, 2024
+101
−29
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7fcc549
Implement IEmailSenderClient interface and implementation
kasparboelkjeldsen 867b74e
fix test
kasparboelkjeldsen d9c5825
Documentation
kasparboelkjeldsen 92f5c2c
EmailMessageExtensions public, use EmailMessage in interface and impl.
kasparboelkjeldsen f450b85
move mimemessage into implementation
kasparboelkjeldsen ba68ffd
revert EmailMessageExtensions back to internal
kasparboelkjeldsen 9e38c16
use StaticServiceProvider to avoid breaking change
kasparboelkjeldsen 1942b60
Fix test after changing constructor
kasparboelkjeldsen c667f72
revert constructor change and add new constructor an obsoletes
kasparboelkjeldsen 0f51b84
Moved a paranthesis so it will build in release-mode
kasparboelkjeldsen 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
50 changes: 50 additions & 0 deletions
50
src/Umbraco.Infrastructure/Mail/BasicSmtpEmailSenderClient.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,50 @@ | ||
using System.Net.Mail; | ||
using Microsoft.Extensions.Options; | ||
using Umbraco.Cms.Core.Configuration.Models; | ||
using Umbraco.Cms.Core.Models.Email; | ||
using Umbraco.Cms.Infrastructure.Extensions; | ||
using Umbraco.Cms.Infrastructure.Mail.Interfaces; | ||
using SecureSocketOptions = MailKit.Security.SecureSocketOptions; | ||
using SmtpClient = MailKit.Net.Smtp.SmtpClient; | ||
|
||
namespace Umbraco.Cms.Infrastructure.Mail | ||
{ | ||
/// <summary> | ||
/// A basic SMTP email sender client using MailKits SMTP client. | ||
/// </summary> | ||
public class BasicSmtpEmailSenderClient : IEmailSenderClient | ||
{ | ||
private readonly GlobalSettings _globalSettings; | ||
public BasicSmtpEmailSenderClient(IOptionsMonitor<GlobalSettings> globalSettings) | ||
{ | ||
_globalSettings = globalSettings.CurrentValue; | ||
} | ||
|
||
public async Task SendAsync(EmailMessage message) | ||
{ | ||
using var client = new SmtpClient(); | ||
|
||
await client.ConnectAsync( | ||
_globalSettings.Smtp!.Host, | ||
_globalSettings.Smtp.Port, | ||
(SecureSocketOptions)(int)_globalSettings.Smtp.SecureSocketOptions); | ||
|
||
if (!string.IsNullOrWhiteSpace(_globalSettings.Smtp.Username) && | ||
!string.IsNullOrWhiteSpace(_globalSettings.Smtp.Password)) | ||
{ | ||
await client.AuthenticateAsync(_globalSettings.Smtp.Username, _globalSettings.Smtp.Password); | ||
} | ||
|
||
var mimeMessage = message.ToMimeMessage(_globalSettings.Smtp!.From); | ||
|
||
if (_globalSettings.Smtp.DeliveryMethod == SmtpDeliveryMethod.Network) | ||
{ | ||
await client.SendAsync(mimeMessage); | ||
} | ||
else | ||
{ | ||
client.Send(mimeMessage); | ||
} | ||
} | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/Umbraco.Infrastructure/Mail/Interfaces/IEmailSenderClient.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,17 @@ | ||
using Umbraco.Cms.Core.Models.Email; | ||
|
||
namespace Umbraco.Cms.Infrastructure.Mail.Interfaces | ||
{ | ||
/// <summary> | ||
/// Client for sending an email from a MimeMessage | ||
/// </summary> | ||
public interface IEmailSenderClient | ||
{ | ||
/// <summary> | ||
/// Sends the email message | ||
/// </summary> | ||
/// <param name="message"></param> | ||
/// <returns></returns> | ||
public Task SendAsync(EmailMessage message); | ||
} | ||
} |
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
Oops, something went wrong.
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.
Besides the breaking changes it looks good now.
Please add the original constructor signatures and use
StaticServiceProvider.Instance.GetRequiredService<IEmailSenderClient>()
where neededThere 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.
Gotcha - it's back to normal now and using StaticServiceProvider instead
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.
Sorry, I was not clear enough.
We usally make the constructors as we want them, but call the new constructor from the old, and input missing dependencies using the static service provider and obsolete the old one. These can be obsoleted and removed in Umbraco 17, now that they will not be part of Umbraco 15.0.0
Example here
https://github.com/umbraco/Umbraco-CMS/blob/contrib/src/Umbraco.Cms.Api.Delivery/Controllers/Security/MemberController.cs#L37-L58
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.
@bergmania - no I'm sorry, I've actually had to do that in another pull request so I should have guessed it.
Here is another attempt, though speaking of constructors, I'm a bit perplexed by the shorthanded constructor
as it has no references to it in the entire code base? If it's there for backwards compatibility, I suppose it should also be obsolete ? That's what I've done anyway. Hope I'm right.