#8142 - Small cleanup, and bugfix
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Net.Mail;
|
||||
using Umbraco.Core.Configuration;
|
||||
|
||||
namespace Umbraco.Configuration
|
||||
@@ -8,6 +9,10 @@ namespace Umbraco.Configuration
|
||||
public string Host { get; set; }
|
||||
public int Port { get; set; }
|
||||
public string PickupDirectoryLocation { get; set; }
|
||||
public string DeliveryMethod { get; set; }
|
||||
public SmtpDeliveryMethod DeliveryMethod { get; set; }
|
||||
|
||||
public string Username { get; set; }
|
||||
|
||||
public string Password { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Mail;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Configuration;
|
||||
@@ -90,7 +91,11 @@ namespace Umbraco.Configuration.Models
|
||||
public string Host => _configurationSection.GetValue<string>("Host");
|
||||
public int Port => _configurationSection.GetValue<int>("Port");
|
||||
public string PickupDirectoryLocation => _configurationSection.GetValue<string>("PickupDirectoryLocation");
|
||||
public string DeliveryMethod => _configurationSection.GetValue<string>("DeliveryMethod");
|
||||
public SmtpDeliveryMethod DeliveryMethod => _configurationSection.GetValue<SmtpDeliveryMethod>("DeliveryMethod");
|
||||
|
||||
public string Username => _configurationSection.GetValue<string>("Username");
|
||||
|
||||
public string Password => _configurationSection.GetValue<string>("Password");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace Umbraco.Core.Configuration
|
||||
{
|
||||
public interface ISmtpSettings
|
||||
@@ -6,6 +8,8 @@ namespace Umbraco.Core.Configuration
|
||||
string Host { get; }
|
||||
int Port{ get; }
|
||||
string PickupDirectoryLocation { get; }
|
||||
string DeliveryMethod { get; }
|
||||
SmtpDeliveryMethod DeliveryMethod { get; }
|
||||
string Username { get; }
|
||||
string Password { get; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Mail;
|
||||
using System.Threading.Tasks;
|
||||
using MailKit.Security;
|
||||
using MimeKit;
|
||||
using MimeKit.Text;
|
||||
using Umbraco.Core.Configuration;
|
||||
@@ -70,18 +70,16 @@ namespace Umbraco.Core
|
||||
{
|
||||
using (var client = new SmtpClient())
|
||||
{
|
||||
var smtpSettingsDeliveryMethod = _globalSettings.SmtpSettings.DeliveryMethod;
|
||||
var deliveryMethod = (SmtpDeliveryMethod)Enum.Parse(typeof(SmtpDeliveryMethod), smtpSettingsDeliveryMethod, true);
|
||||
|
||||
await client.ConnectAsync(_globalSettings.SmtpSettings.Host, _globalSettings.SmtpSettings.Port);
|
||||
|
||||
if (deliveryMethod == SmtpDeliveryMethod.Network)
|
||||
var mailMessage = ConstructEmailMessage(message);
|
||||
if (_globalSettings.SmtpSettings.DeliveryMethod == SmtpDeliveryMethod.Network)
|
||||
{
|
||||
await client.SendAsync(ConstructEmailMessage(message));
|
||||
await client.SendAsync(mailMessage);
|
||||
}
|
||||
else
|
||||
{
|
||||
client.Send(ConstructEmailMessage(message));
|
||||
client.Send(mailMessage);
|
||||
}
|
||||
|
||||
await client.DisconnectAsync(true);
|
||||
@@ -118,24 +116,17 @@ namespace Umbraco.Core
|
||||
|
||||
private MimeMessage ConstructEmailMessage(MailMessage mailMessage)
|
||||
{
|
||||
var messageToSend = new MimeMessage
|
||||
{
|
||||
Subject = mailMessage.Subject
|
||||
};
|
||||
|
||||
var fromEmail = mailMessage.From?.Address;
|
||||
if(string.IsNullOrEmpty(fromEmail))
|
||||
fromEmail = _globalSettings.SmtpSettings.From;
|
||||
|
||||
messageToSend.From.Add(new MailboxAddress(fromEmail));
|
||||
|
||||
foreach (var mailAddress in mailMessage.To)
|
||||
messageToSend.To.Add(new MailboxAddress(mailAddress.Address));
|
||||
|
||||
if (mailMessage.IsBodyHtml)
|
||||
messageToSend.Body = new TextPart(TextFormat.Html) { Text = mailMessage.Body };
|
||||
else
|
||||
messageToSend.Body = new TextPart(TextFormat.Plain) { Text = mailMessage.Body };
|
||||
var messageToSend = new MimeMessage
|
||||
{
|
||||
Subject = mailMessage.Subject,
|
||||
From = { new MailboxAddress(fromEmail)},
|
||||
Body = new TextPart(mailMessage.IsBodyHtml ? TextFormat.Html : TextFormat.Plain) { Text = mailMessage.Body }
|
||||
};
|
||||
messageToSend.To.AddRange(mailMessage.To.Select(x=>new MailboxAddress(x.Address)));
|
||||
|
||||
return messageToSend;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Umbraco.Core.Configuration;
|
||||
using System.Net.Mail;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Models.Membership;
|
||||
|
||||
namespace Umbraco.Tests.Common.Builders
|
||||
{
|
||||
@@ -16,7 +18,9 @@ namespace Umbraco.Tests.Common.Builders
|
||||
private string _host;
|
||||
private int? _port;
|
||||
private string _pickupDirectoryLocation;
|
||||
private string _deliveryMethod;
|
||||
private SmtpDeliveryMethod? _deliveryMethod;
|
||||
private string _username;
|
||||
private string _password;
|
||||
|
||||
public SmtpSettingsBuilder(TParent parentBuilder) : base(parentBuilder)
|
||||
{
|
||||
@@ -34,19 +38,31 @@ namespace Umbraco.Tests.Common.Builders
|
||||
return this;
|
||||
}
|
||||
|
||||
public SmtpSettingsBuilder<TParent> WithUsername(string username)
|
||||
{
|
||||
_username = username;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SmtpSettingsBuilder<TParent> WithPost(int port)
|
||||
{
|
||||
_port = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SmtpSettingsBuilder<TParent> WithPassword(string password)
|
||||
{
|
||||
_password = password;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SmtpSettingsBuilder<TParent> WithPickupDirectoryLocation(string pickupDirectoryLocation)
|
||||
{
|
||||
_pickupDirectoryLocation = pickupDirectoryLocation;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SmtpSettingsBuilder<TParent> WithDeliveryMethod(string deliveryMethod)
|
||||
public SmtpSettingsBuilder<TParent> WithDeliveryMethod(SmtpDeliveryMethod deliveryMethod)
|
||||
{
|
||||
_deliveryMethod = deliveryMethod;
|
||||
return this;
|
||||
@@ -58,7 +74,9 @@ namespace Umbraco.Tests.Common.Builders
|
||||
var host = _host ?? null;
|
||||
var port = _port ?? 25;
|
||||
var pickupDirectoryLocation = _pickupDirectoryLocation ?? null;
|
||||
var deliveryMethod = _deliveryMethod ?? null;
|
||||
var deliveryMethod = _deliveryMethod ?? SmtpDeliveryMethod.Network;
|
||||
var username = _username ?? null;
|
||||
var password = _password ?? null;
|
||||
|
||||
return new TestSmtpSettings()
|
||||
{
|
||||
@@ -66,7 +84,9 @@ namespace Umbraco.Tests.Common.Builders
|
||||
Host = host,
|
||||
Port = port,
|
||||
PickupDirectoryLocation = pickupDirectoryLocation,
|
||||
DeliveryMethod = deliveryMethod
|
||||
DeliveryMethod = deliveryMethod,
|
||||
Username = username,
|
||||
Password = password,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -76,7 +96,9 @@ namespace Umbraco.Tests.Common.Builders
|
||||
public string Host { get; set; }
|
||||
public int Port { get; set; }
|
||||
public string PickupDirectoryLocation { get; set; }
|
||||
public string DeliveryMethod { get; set; }
|
||||
public SmtpDeliveryMethod DeliveryMethod { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -313,6 +313,7 @@ namespace Umbraco.Tests.Testing
|
||||
Composition.RegisterUnique<ISectionService, SectionService>();
|
||||
|
||||
Composition.RegisterUnique<HtmlLocalLinkParser>();
|
||||
Composition.RegisterUnique<IEmailSender, EmailSender>();
|
||||
Composition.RegisterUnique<HtmlUrlParser>();
|
||||
Composition.RegisterUnique<HtmlImageSourceParser>();
|
||||
Composition.RegisterUnique<RichTextEditorPastedImages>();
|
||||
|
||||
@@ -91,7 +91,8 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
Factory.GetInstance<IRuntimeState>(),
|
||||
Factory.GetInstance<UmbracoMapper>(),
|
||||
Factory.GetInstance<ISecuritySettings>(),
|
||||
Factory.GetInstance<IPublishedUrlProvider>()
|
||||
Factory.GetInstance<IPublishedUrlProvider>(),
|
||||
Factory.GetInstance<IEmailSender>()
|
||||
);
|
||||
return usersController;
|
||||
}
|
||||
|
||||
@@ -103,7 +103,8 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
Factory.GetInstance<IHostingEnvironment>(),
|
||||
Factory.GetInstance<IImageUrlGenerator>(),
|
||||
Factory.GetInstance<IPublishedUrlProvider>(),
|
||||
Factory.GetInstance<ISecuritySettings>()
|
||||
Factory.GetInstance<ISecuritySettings>(),
|
||||
Factory.GetInstance<IEmailSender>()
|
||||
|
||||
);
|
||||
return usersController;
|
||||
@@ -176,7 +177,8 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
Factory.GetInstance<IHostingEnvironment>(),
|
||||
Factory.GetInstance<IImageUrlGenerator>(),
|
||||
Factory.GetInstance<IPublishedUrlProvider>(),
|
||||
Factory.GetInstance<ISecuritySettings>()
|
||||
Factory.GetInstance<ISecuritySettings>(),
|
||||
Factory.GetInstance<IEmailSender>()
|
||||
);
|
||||
return usersController;
|
||||
}
|
||||
@@ -219,7 +221,8 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
Factory.GetInstance<IHostingEnvironment>(),
|
||||
Factory.GetInstance<IImageUrlGenerator>(),
|
||||
Factory.GetInstance<IPublishedUrlProvider>(),
|
||||
Factory.GetInstance<ISecuritySettings>()
|
||||
Factory.GetInstance<ISecuritySettings>(),
|
||||
Factory.GetInstance<IEmailSender>()
|
||||
);
|
||||
return usersController;
|
||||
}
|
||||
@@ -297,7 +300,8 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
Factory.GetInstance<IHostingEnvironment>(),
|
||||
Factory.GetInstance<IImageUrlGenerator>(),
|
||||
Factory.GetInstance<IPublishedUrlProvider>(),
|
||||
Factory.GetInstance<ISecuritySettings>()
|
||||
Factory.GetInstance<ISecuritySettings>(),
|
||||
Factory.GetInstance<IEmailSender>()
|
||||
);
|
||||
return usersController;
|
||||
}
|
||||
@@ -487,7 +491,8 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
Factory.GetInstance<IHostingEnvironment>(),
|
||||
Factory.GetInstance<IImageUrlGenerator>(),
|
||||
Factory.GetInstance<IPublishedUrlProvider>(),
|
||||
Factory.GetInstance<ISecuritySettings>());
|
||||
Factory.GetInstance<ISecuritySettings>(),
|
||||
Factory.GetInstance<IEmailSender>());
|
||||
|
||||
var mockOwinContext = new Mock<IOwinContext>();
|
||||
var mockUserManagerMarker = new Mock<IBackOfficeUserManagerMarker>();
|
||||
|
||||
@@ -48,6 +48,7 @@ namespace Umbraco.Web.Editors
|
||||
private readonly IHostingEnvironment _hostingEnvironment;
|
||||
private readonly IRuntimeState _runtimeState;
|
||||
private readonly ISecuritySettings _securitySettings;
|
||||
private readonly IEmailSender _emailSender;
|
||||
|
||||
public AuthenticationController(
|
||||
IUserPasswordConfiguration passwordConfiguration,
|
||||
@@ -61,13 +62,15 @@ namespace Umbraco.Web.Editors
|
||||
IRuntimeState runtimeState,
|
||||
UmbracoMapper umbracoMapper,
|
||||
ISecuritySettings securitySettings,
|
||||
IPublishedUrlProvider publishedUrlProvider)
|
||||
IPublishedUrlProvider publishedUrlProvider,
|
||||
IEmailSender emailSender)
|
||||
: base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, umbracoMapper, publishedUrlProvider)
|
||||
{
|
||||
_passwordConfiguration = passwordConfiguration ?? throw new ArgumentNullException(nameof(passwordConfiguration));
|
||||
_hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException(nameof(hostingEnvironment));
|
||||
_runtimeState = runtimeState ?? throw new ArgumentNullException(nameof(runtimeState));
|
||||
_securitySettings = securitySettings ?? throw new ArgumentNullException(nameof(securitySettings));
|
||||
_emailSender = emailSender;
|
||||
}
|
||||
|
||||
protected BackOfficeUserManager<BackOfficeIdentityUser> UserManager => _userManager
|
||||
@@ -333,16 +336,15 @@ namespace Umbraco.Web.Editors
|
||||
// Ensure the culture of the found user is used for the email!
|
||||
UmbracoUserExtensions.GetUserCulture(identityUser.Culture, Services.TextService, GlobalSettings));
|
||||
|
||||
var emailSender = new EmailSender(GlobalSettings, true);
|
||||
var mailMessage = new MailMessage()
|
||||
{
|
||||
Subject = subject,
|
||||
Body = message,
|
||||
IsBodyHtml = true
|
||||
IsBodyHtml = true,
|
||||
To = { user.Email}
|
||||
};
|
||||
mailMessage.To.Add(user.Email);
|
||||
|
||||
await emailSender.SendAsync(mailMessage);
|
||||
await _emailSender.SendAsync(mailMessage);
|
||||
|
||||
UserManager.RaiseForgotPasswordRequestedEvent(user.Id);
|
||||
}
|
||||
|
||||
@@ -53,6 +53,7 @@ namespace Umbraco.Web.Editors
|
||||
private readonly ISqlContext _sqlContext;
|
||||
private readonly IImageUrlGenerator _imageUrlGenerator;
|
||||
private readonly ISecuritySettings _securitySettings;
|
||||
private readonly IEmailSender _emailSender;
|
||||
|
||||
public UsersController(
|
||||
IGlobalSettings globalSettings,
|
||||
@@ -69,7 +70,8 @@ namespace Umbraco.Web.Editors
|
||||
IHostingEnvironment hostingEnvironment,
|
||||
IImageUrlGenerator imageUrlGenerator,
|
||||
IPublishedUrlProvider publishedUrlProvider,
|
||||
ISecuritySettings securitySettings)
|
||||
ISecuritySettings securitySettings,
|
||||
IEmailSender emailSender)
|
||||
: base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, shortStringHelper, umbracoMapper, publishedUrlProvider)
|
||||
{
|
||||
_mediaFileSystem = mediaFileSystem;
|
||||
@@ -78,6 +80,7 @@ namespace Umbraco.Web.Editors
|
||||
_sqlContext = sqlContext;
|
||||
_imageUrlGenerator = imageUrlGenerator;
|
||||
_securitySettings = securitySettings;
|
||||
_emailSender = emailSender;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -501,16 +504,15 @@ namespace Umbraco.Web.Editors
|
||||
UmbracoUserExtensions.GetUserCulture(to.Language, Services.TextService, GlobalSettings),
|
||||
new[] { userDisplay.Name, from, message, inviteUri.ToString(), fromEmail });
|
||||
|
||||
var emailSender = new EmailSender(GlobalSettings, true);
|
||||
var mailMessage = new MailMessage()
|
||||
{
|
||||
Subject = emailSubject,
|
||||
Body = emailBody,
|
||||
IsBodyHtml = true
|
||||
IsBodyHtml = true,
|
||||
To = { to.Email}
|
||||
};
|
||||
mailMessage.To.Add(to.Email);
|
||||
|
||||
await emailSender.SendAsync(mailMessage);
|
||||
await _emailSender.SendAsync(mailMessage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user