Merge pull request #12606 from abpframework/liangshiwei/emailsettigns

Sending test email in the setting page with MVC and Blazor UI
pull/12621/head
maliming 3 years ago committed by GitHub
commit 8009f0f121
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,4 +8,6 @@ public interface IEmailSettingsAppService : IApplicationService
Task<EmailSettingsDto> GetAsync();
Task UpdateAsync(UpdateEmailSettingsDto input);
Task SendTestEmailAsync(SendTestEmailInput input);
}

@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
namespace Volo.Abp.SettingManagement;
public class SendTestEmailInput
{
[Required]
public string SenderEmailAddress { get; set; }
[Required]
public string TargetEmailAddress { get; set; }
[Required]
public string Subject { get; set; }
public string Body { get; set; }
}

@ -10,9 +10,11 @@ public class SettingManagementPermissionDefinitionProvider : PermissionDefinitio
{
var moduleGroup = context.AddGroup(SettingManagementPermissions.GroupName, L("Permission:SettingManagement"));
moduleGroup
.AddPermission(SettingManagementPermissions.Emailing, L("Permission:Emailing"))
.StateCheckers.Add(new AllowTenantsToChangeEmailSettingsFeatureSimpleStateChecker());
var emailPermission = moduleGroup
.AddPermission(SettingManagementPermissions.Emailing, L("Permission:Emailing"));
emailPermission.StateCheckers.Add(new AllowTenantsToChangeEmailSettingsFeatureSimpleStateChecker());
emailPermission.AddChild(SettingManagementPermissions.EmailingTest, L("Permission:EmailingTest"));
}
private static LocalizableString L(string name)

@ -7,6 +7,8 @@ public class SettingManagementPermissions
public const string GroupName = "SettingManagement";
public const string Emailing = GroupName + ".Emailing";
public const string EmailingTest = Emailing + ".Test";
public static string[] GetAll()
{

@ -11,10 +11,13 @@ namespace Volo.Abp.SettingManagement;
public class EmailSettingsAppService : SettingManagementAppServiceBase, IEmailSettingsAppService
{
protected ISettingManager SettingManager { get; }
protected IEmailSender EmailSender { get; }
public EmailSettingsAppService(ISettingManager settingManager)
public EmailSettingsAppService(ISettingManager settingManager, IEmailSender emailSender)
{
SettingManager = settingManager;
EmailSender = emailSender;
}
public virtual async Task<EmailSettingsDto> GetAsync()
@ -60,6 +63,14 @@ public class EmailSettingsAppService : SettingManagementAppServiceBase, IEmailSe
await SettingManager.SetForTenantOrGlobalAsync(CurrentTenant.Id, EmailSettingNames.DefaultFromDisplayName, input.DefaultFromDisplayName);
}
[Authorize(SettingManagementPermissions.EmailingTest)]
public virtual async Task SendTestEmailAsync(SendTestEmailInput input)
{
await CheckFeatureAsync();
await EmailSender.SendAsync(input.SenderEmailAddress, input.TargetEmailAddress, input.Subject, input.Body);
}
protected virtual async Task CheckFeatureAsync()
{
await FeatureChecker.CheckEnabledAsync(SettingManagementFeatures.Enable);

@ -13,7 +13,7 @@
<FieldLabel>@L["DefaultFromDisplayName"] *</FieldLabel>
<TextEdit @bind-Text="@EmailSettings.DefaultFromDisplayName">
<Feedback>
<ValidationError />
<ValidationError/>
</Feedback>
</TextEdit>
</Field>
@ -23,17 +23,17 @@
<FieldLabel>@L["DefaultFromAddress"] *</FieldLabel>
<TextEdit @bind-Text="@EmailSettings.DefaultFromAddress">
<Feedback>
<ValidationError />
<ValidationError/>
</Feedback>
</TextEdit>
</Field>
</Validation>
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["SmtpHost"]</FieldLabel>
<FieldLabel>@L["SmtpHost"]</FieldLabel>
<TextEdit @bind-Text="@EmailSettings.SmtpHost">
<Feedback>
<ValidationError />
<ValidationError/>
</Feedback>
</TextEdit>
</Field>
@ -58,30 +58,30 @@
{
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["SmtpDomain"]</FieldLabel>
<FieldLabel>@L["SmtpDomain"]</FieldLabel>
<TextEdit @bind-Text="@EmailSettings.SmtpDomain">
<Feedback>
<ValidationError />
<ValidationError/>
</Feedback>
</TextEdit>
</Field>
</Validation>
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["SmtpUserName"]</FieldLabel>
<FieldLabel>@L["SmtpUserName"]</FieldLabel>
<TextEdit @bind-Text="@EmailSettings.SmtpUserName">
<Feedback>
<ValidationError />
<ValidationError/>
</Feedback>
</TextEdit>
</Field>
</Validation>
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["SmtpPassword"]</FieldLabel>
<FieldLabel>@L["SmtpPassword"]</FieldLabel>
<TextEdit Role="TextRole.Password" @bind-Text="@EmailSettings.SmtpPassword">
<Feedback>
<ValidationError />
<ValidationError/>
</Feedback>
</TextEdit>
</Field>
@ -92,8 +92,75 @@
</Validations>
<Row>
<Column>
<SubmitButton Clicked="@UpdateSettingsAsync" />
<SubmitButton Clicked="@UpdateSettingsAsync"/>
@if (HasSendTestEmailPermission)
{
<Button style="margin: 0.25rem" Color="Color.Primary" Clicked="OpenSendTestEmailModalAsync">@L["SendTestEmail"]</Button>
}
</Column>
</Row>
</Form>
}
@if (HasSendTestEmailPermission)
{
<Modal @ref="SendTestEmailModal">
<ModalContent Centered="true">
<Form>
<ModalHeader>
<ModalTitle>@L["SendTestEmail"]</ModalTitle>
<CloseButton Clicked="CloseSendTestEmailModalAsync"/>
</ModalHeader>
<ModalBody>
<Validations @ref="@EmailSettingTestValidation" Model="@SendTestEmailInput" ValidateOnLoad="false">
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["SenderEmailAddress"]</FieldLabel>
<TextEdit @bind-Text="@SendTestEmailInput.SenderEmailAddress" Autofocus="true">
<Feedback>
<ValidationError/>
</Feedback>
</TextEdit>
</Field>
</Validation>
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["TargetEmailAddress"]</FieldLabel>
<TextEdit @bind-Text="@SendTestEmailInput.TargetEmailAddress">
<Feedback>
<ValidationError/>
</Feedback>
</TextEdit>
</Field>
</Validation>
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["Subject"]</FieldLabel>
<TextEdit @bind-Text="@SendTestEmailInput.Subject">
<Feedback>
<ValidationError/>
</Feedback>
</TextEdit>
</Field>
</Validation>
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["Body"]</FieldLabel>
<TextEdit @bind-Text="@SendTestEmailInput.Body">
<Feedback>
<ValidationError/>
</Feedback>
</TextEdit>
</Field>
</Validation>
</Validations>
</ModalBody>
<ModalFooter>
<Button Color="Color.Secondary" Clicked="CloseSendTestEmailModalAsync">@L["Cancel"]</Button>
<Button Color="Color.Primary" Clicked="SendTestEmailAsync">@L["Send"]</Button>
</ModalFooter>
</Form>
</ModalContent>
</Modal>
}
}

@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Components;
using Volo.Abp.AspNetCore.Components.Messages;
using Volo.Abp.AspNetCore.Components.Web.Configuration;
using Volo.Abp.Auditing;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.SettingManagement.Localization;
namespace Volo.Abp.SettingManagement.Blazor.Pages.SettingManagement.EmailSettingGroup;
@ -14,6 +15,9 @@ public partial class EmailSettingGroupViewComponent
{
[Inject]
protected IEmailSettingsAppService EmailSettingsAppService { get; set; }
[Inject]
protected IPermissionChecker PermissionChecker { get; set; }
[Inject]
private ICurrentApplicationConfigurationCacheResetService CurrentApplicationConfigurationCacheResetService { get; set; }
@ -23,7 +27,17 @@ public partial class EmailSettingGroupViewComponent
protected UpdateEmailSettingsViewModel EmailSettings;
protected SendTestEmailViewModel SendTestEmailInput;
protected Validations EmailSettingValidation;
protected Validations EmailSettingTestValidation;
protected Modal SendTestEmailModal;
protected bool HasSendTestEmailPermission { get; set; }
public EmailSettingGroupViewComponent()
{
@ -36,6 +50,8 @@ public partial class EmailSettingGroupViewComponent
try
{
EmailSettings = ObjectMapper.Map<EmailSettingsDto, UpdateEmailSettingsViewModel>(await EmailSettingsAppService.GetAsync());
HasSendTestEmailPermission = await PermissionChecker.IsGrantedAsync(SettingManagementPermissions.EmailingTest);
SendTestEmailInput = new SendTestEmailViewModel();
}
catch (Exception ex)
{
@ -64,6 +80,52 @@ public partial class EmailSettingGroupViewComponent
}
}
protected virtual async Task OpenSendTestEmailModalAsync()
{
try
{
await EmailSettingTestValidation.ClearAll();
var emailSettings = await EmailSettingsAppService.GetAsync();
SendTestEmailInput = new SendTestEmailViewModel
{
SenderEmailAddress = emailSettings.DefaultFromAddress,
TargetEmailAddress = CurrentUser.Email,
Subject = L["TestEmailSubject", new Random().Next(1000, 9999)],
Body = L["TestEmailBody"]
};
await SendTestEmailModal.Show();
}
catch (Exception ex)
{
await HandleErrorAsync(ex);
}
}
protected virtual Task CloseSendTestEmailModalAsync()
{
return InvokeAsync(SendTestEmailModal.Hide);
}
protected virtual async Task SendTestEmailAsync()
{
try
{
if (!await EmailSettingTestValidation.ValidateAll())
{
return;
}
await EmailSettingsAppService.SendTestEmailAsync(ObjectMapper.Map<SendTestEmailViewModel, SendTestEmailInput>(SendTestEmailInput));
await Notify.Success(L["SuccessfullySent"]);
}
catch (Exception ex)
{
await HandleErrorAsync(ex);
}
}
public class UpdateEmailSettingsViewModel
{
[MaxLength(256)]
@ -104,4 +166,18 @@ public partial class EmailSettingGroupViewComponent
[Display(Name = "DefaultFromDisplayName")]
public string DefaultFromDisplayName { get; set; }
}
public class SendTestEmailViewModel
{
[Required]
public string SenderEmailAddress { get; set; }
[Required]
public string TargetEmailAddress { get; set; }
[Required]
public string Subject { get; set; }
public string Body { get; set; }
}
}

@ -9,5 +9,7 @@ public class SettingManagementBlazorAutoMapperProfile : Profile
{
CreateMap<EmailSettingGroupViewComponent.UpdateEmailSettingsViewModel, UpdateEmailSettingsDto>();
CreateMap<EmailSettingsDto, EmailSettingGroupViewComponent.UpdateEmailSettingsViewModel>();
CreateMap<EmailSettingGroupViewComponent.SendTestEmailViewModel, SendTestEmailInput>();
}
}

@ -5,6 +5,16 @@
"SuccessfullySaved": "Successfully saved",
"Permission:SettingManagement": "Setting Management",
"Permission:Emailing": "Emailing",
"Permission:EmailingTest": "Emailing test",
"SendTestEmail": "Send test email",
"SenderEmailAddress": "Sender email address",
"TargetEmailAddress": "Target email address",
"Subject": "Subject",
"Body": "Body",
"TestEmailSubject": "Test email {0}",
"TestEmailBody": "Test email body message here",
"SuccessfullySent": "Successfully sent",
"Send": "Send",
"Menu:Emailing": "Emailing",
"SmtpHost": "Host",
"SmtpPort": "Port",

@ -5,6 +5,16 @@
"SuccessfullySaved": "Başarıyla Kaydedildi",
"Permission:SettingManagement": "Ayarlar yönetimi",
"Permission:Emailing": "Email",
"Permission:EmailingTest": "Email testi",
"SendTestEmail": "Test emaili gönder",
"SenderEmailAddress": "Gönderen email adresi",
"TargetEmailAddress": "Hedef email adresi",
"Subject": "Konu",
"Body": "Gövde",
"TestEmailSubject": "Test email {0}",
"TestEmailBody": "E-posta gövdesi mesajını burada test edin",
"SuccessfullySent": "Başarıyla gönderildi",
"Send": "Gönder",
"Menu:Emailing": "Email",
"SmtpHost": "Sunucu",
"SmtpPort": "Port",

@ -5,6 +5,16 @@
"SuccessfullySaved": "保存成功",
"Permission:SettingManagement": "设置管理",
"Permission:Emailing": "邮件",
"Permission:EmailingTest": "邮件测试",
"SendTestEmail": "发送测试邮件",
"SenderEmailAddress": "发件人邮箱地址",
"TargetEmailAddress": "收件人邮箱地址",
"Subject": "主题",
"Body": "正文",
"TestEmailSubject": "测试邮件 {0}",
"TestEmailBody": "测试邮件内容",
"SuccessfullySent": "发送成功",
"Send": "发送",
"Menu:Emailing": "邮件",
"SmtpHost": "主机",
"SmtpPort": "端口",

@ -5,6 +5,16 @@
"SuccessfullySaved": "保存成功",
"Permission:SettingManagement": "設定管理",
"Permission:Emailing": "信箱",
"Permission:EmailingTest": "邮件測試",
"SendTestEmail": "發送測試邮件",
"SenderEmailAddress": "發送者電子郵件地址",
"TargetEmailAddress": "目標電子郵件地址",
"Subject": "主題",
"Body": "體",
"TestEmailSubject": "測試邮件 {0}",
"TestEmailBody": "測試邮件內容",
"SuccessfullySent": "成功發送",
"Send": "發送",
"Menu:Emailing": "信箱",
"SmtpHost": "主機",
"SmtpPort": "Port",

@ -27,4 +27,12 @@ public partial class EmailSettingsClientProxy : ClientProxyBase<IEmailSettingsAp
{ typeof(UpdateEmailSettingsDto), input }
});
}
public virtual async Task SendTestEmailAsync(SendTestEmailInput input)
{
await RequestAsync(nameof(SendTestEmailAsync), new ClientProxyRequestTypeValue
{
{ typeof(SendTestEmailInput), input }
});
}
}

@ -7,6 +7,8 @@
"Volo.Abp.SettingManagement.EmailSettingsController": {
"controllerName": "EmailSettings",
"controllerGroupName": "EmailSettings",
"isRemoteService": true,
"apiVersion": null,
"type": "Volo.Abp.SettingManagement.EmailSettingsController",
"interfaces": [
{
@ -65,6 +67,43 @@
},
"allowAnonymous": null,
"implementFrom": "Volo.Abp.SettingManagement.IEmailSettingsAppService"
},
"SendTestEmailAsyncByInput": {
"uniqueName": "SendTestEmailAsyncByInput",
"name": "SendTestEmailAsync",
"httpMethod": "POST",
"url": "api/setting-management/emailing/send-test-email",
"supportedVersions": [],
"parametersOnMethod": [
{
"name": "input",
"typeAsString": "Volo.Abp.SettingManagement.SendTestEmailInput, Volo.Abp.SettingManagement.Application.Contracts",
"type": "Volo.Abp.SettingManagement.SendTestEmailInput",
"typeSimple": "Volo.Abp.SettingManagement.SendTestEmailInput",
"isOptional": false,
"defaultValue": null
}
],
"parameters": [
{
"nameOnMethod": "input",
"name": "input",
"jsonName": null,
"type": "Volo.Abp.SettingManagement.SendTestEmailInput",
"typeSimple": "Volo.Abp.SettingManagement.SendTestEmailInput",
"isOptional": false,
"defaultValue": null,
"constraintTypes": null,
"bindingSourceId": "Body",
"descriptorName": ""
}
],
"returnValue": {
"type": "System.Void",
"typeSimple": "System.Void"
},
"allowAnonymous": null,
"implementFrom": "Volo.Abp.SettingManagement.IEmailSettingsAppService"
}
}
}

@ -27,4 +27,10 @@ public class EmailSettingsController : AbpControllerBase, IEmailSettingsAppServi
{
return _emailSettingsAppService.UpdateAsync(input);
}
[HttpPost("send-test-email")]
public Task SendTestEmailAsync(SendTestEmailInput input)
{
return _emailSettingsAppService.SendTestEmailAsync(input);
}
}

@ -1,6 +1,9 @@
@using Microsoft.AspNetCore.Mvc.Localization
@using Volo.Abp.Authorization.Permissions
@using Volo.Abp.SettingManagement
@using Volo.Abp.SettingManagement.Localization
@inject IHtmlLocalizer<AbpSettingManagementResource> L
@inject IPermissionChecker PermissionChecker
@model Volo.Abp.SettingManagement.Web.Pages.SettingManagement.Components.EmailSettingGroup.EmailSettingGroupViewComponent.UpdateEmailSettingsViewModel
<form id="EmailSettingsForm" method="post">
@ -24,6 +27,12 @@
<abp-button button-type="Primary" type="submit">
<i class="fa fa-save"></i> @L["Save"]
</abp-button>
@if (await PermissionChecker.IsGrantedAsync(SettingManagementPermissions.EmailingTest))
{
<abp-button button-type="Primary" id="SendTestEmailButton">
<i class="fa fa-send"></i> @L["SendTestEmail"]
</abp-button>
}
</div>
</abp-column>
</abp-row>

@ -1,16 +1,20 @@
(function ($) {
var _sendTestEmailModal = new abp.ModalManager(
abp.appPath + 'SettingManagement/Components/EmailSettingGroup/SendTestEmailModal'
);
$(function () {
var l = abp.localization.getResource('AbpSettingManagement');
$("#EmailSettingsForm").on('submit', function (event) {
event.preventDefault();
if(!$(this).valid()) {
if (!$(this).valid()) {
return;
}
var form = $(this).serializeFormToObject();
volo.abp.settingManagement.emailSettings.update(form).then(function (result) {
$(document).trigger("AbpSettingSaved");
@ -21,11 +25,28 @@
$('#SmtpUseDefaultCredentials').change(function () {
if (this.checked) {
$('#HideSectionWhenUseDefaultCredentialsIsChecked').slideUp();
}
else {
} else {
$('#HideSectionWhenUseDefaultCredentialsIsChecked').slideDown();
}
});
_sendTestEmailModal.onOpen(function () {
var $form = _sendTestEmailModal.getForm();
_sendTestEmailModal.getForm().off('abp-ajax-success');
$form.on('abp-ajax-success', function () {
_sendTestEmailModal.setResult();
});
})
_sendTestEmailModal.onResult(function () {
abp.notify.success(l('SuccessfullySent'));
});
$("#SendTestEmailButton").click(function (e) {
e.preventDefault();
_sendTestEmailModal.open();
});
});
})(jQuery);

@ -0,0 +1,31 @@
@page
@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.AspNetCore.Mvc.TagHelpers
@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Modal
@using Volo.Abp.SettingManagement.Localization
@using Volo.Abp.SettingManagement.Web.Pages.SettingManagement.Components.EmailSettingGroup
@model SendTestEmailModal
@inject IHtmlLocalizer<AbpSettingManagementResource> L
@{
Layout = null;
}
<form asp-page="/SettingManagement/Components/EmailSettingGroup/SendTestEmailModal">
<abp-modal>
<abp-modal-header title="@L["SendTestEmail"].Value"></abp-modal-header>
<abp-modal-body>
<abp-input asp-for="Input.SenderEmailAddress" />
<abp-input asp-for="Input.TargetEmailAddress" />
<abp-input asp-for="Input.Subject" />
<abp-input asp-for="Input.Body" />
</abp-modal-body>
<abp-modal-footer>
<abp-button data-bs-dismiss="modal" button-type="Secondary">@L["Cancel"]</abp-button>
<abp-button button-type="Primary" type="submit">
<i class="fa fa-send"></i> @L["Send"]
</abp-button>
</abp-modal-footer>
</abp-modal>
</form>

@ -0,0 +1,59 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc.UI.RazorPages;
using Volo.Abp.SettingManagement.Localization;
namespace Volo.Abp.SettingManagement.Web.Pages.SettingManagement.Components.EmailSettingGroup;
[Authorize(SettingManagementPermissions.EmailingTest)]
public class SendTestEmailModal : AbpPageModel
{
[BindProperty]
public SendTestEmailViewModel Input { get; set; }
protected IEmailSettingsAppService EmailSettingsAppService { get; }
public SendTestEmailModal(IEmailSettingsAppService emailSettingsAppService)
{
LocalizationResourceType = typeof(AbpSettingManagementResource);
EmailSettingsAppService = emailSettingsAppService;
}
public async Task OnGetAsync()
{
var emailSettings = await EmailSettingsAppService.GetAsync();
Input = new SendTestEmailViewModel
{
SenderEmailAddress = emailSettings.DefaultFromAddress,
TargetEmailAddress = CurrentUser.Email,
Subject = L["TestEmailSubject", new Random().Next(1000, 9999)],
Body = L["TestEmailBody"]
};
}
public async Task<IActionResult> OnPostAsync()
{
ValidateModel();
await EmailSettingsAppService.SendTestEmailAsync(ObjectMapper.Map<SendTestEmailViewModel, SendTestEmailInput>(Input));
return NoContent();
}
public class SendTestEmailViewModel
{
[Required]
public string SenderEmailAddress { get; set; }
[Required]
public string TargetEmailAddress { get; set; }
[Required]
public string Subject { get; set; }
public string Body { get; set; }
}
}

@ -8,5 +8,7 @@ public class SettingManagementWebAutoMapperProfile : Profile
public SettingManagementWebAutoMapperProfile()
{
CreateMap<EmailSettingsDto, EmailSettingGroupViewComponent.UpdateEmailSettingsViewModel>();
CreateMap<SendTestEmailModal.SendTestEmailViewModel, SendTestEmailInput>();
}
}

@ -27,6 +27,15 @@
}, ajaxParams));
};
volo.abp.settingManagement.emailSettings.sendTestEmail = function(input, ajaxParams) {
return abp.ajax($.extend(true, {
url: abp.appPath + 'api/setting-management/emailing/send-test-email',
type: 'POST',
dataType: null,
data: JSON.stringify(input)
}, ajaxParams));
};
})();
})();

Loading…
Cancel
Save