mirror of https://github.com/abpframework/abp.git
committed by
GitHub
27 changed files with 380 additions and 14 deletions
@ -0,0 +1,9 @@ |
|||||
|
using Newtonsoft.Json; |
||||
|
|
||||
|
namespace Volo.Abp.Json |
||||
|
{ |
||||
|
public class AbpJsonOptions |
||||
|
{ |
||||
|
public JsonSerializerSettings SerializerSettings { get; } = new JsonSerializerSettings(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<Import Project="..\..\..\common.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netstandard2.0</TargetFramework> |
||||
|
<AssemblyName>Volo.Abp.MailKit</AssemblyName> |
||||
|
<PackageId>Volo.Abp.MailKit</PackageId> |
||||
|
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
||||
|
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
||||
|
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
||||
|
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="MailKit" Version="2.2.0" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\Volo.Abp.Emailing\Volo.Abp.Emailing.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,11 @@ |
|||||
|
using Volo.Abp.Emailing; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp.MailKit |
||||
|
{ |
||||
|
[DependsOn(typeof(AbpEmailingModule))] |
||||
|
public class AbpMailKitModule : AbpModule |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using MailKit.Security; |
||||
|
|
||||
|
namespace Volo.Abp.MailKit |
||||
|
{ |
||||
|
public class AbpMailKitOptions |
||||
|
{ |
||||
|
public SecureSocketOptions? SecureSocketOption { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using MailKit.Net.Smtp; |
||||
|
using Volo.Abp.Emailing; |
||||
|
|
||||
|
namespace Volo.Abp.MailKit |
||||
|
{ |
||||
|
public interface IMailKitSmtpEmailSender: IEmailSender |
||||
|
{ |
||||
|
Task<SmtpClient> BuildClientAsync(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,99 @@ |
|||||
|
using System.Net.Mail; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.BackgroundJobs; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Emailing; |
||||
|
using Volo.Abp.Emailing.Smtp; |
||||
|
using MailKit.Security; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using MimeKit; |
||||
|
using Volo.Abp.Threading; |
||||
|
using SmtpClient = MailKit.Net.Smtp.SmtpClient; |
||||
|
|
||||
|
namespace Volo.Abp.MailKit |
||||
|
{ |
||||
|
[Dependency(ServiceLifetime.Transient, ReplaceServices = true)] |
||||
|
public class MailKitSmtpEmailSender : EmailSenderBase, IMailKitSmtpEmailSender |
||||
|
{ |
||||
|
protected AbpMailKitOptions AbpMailKitOptions { get; } |
||||
|
|
||||
|
protected ISmtpEmailSenderConfiguration SmtpConfiguration { get; } |
||||
|
|
||||
|
public MailKitSmtpEmailSender(ISmtpEmailSenderConfiguration smtpConfiguration, |
||||
|
IBackgroundJobManager backgroundJobManager, |
||||
|
IOptions<AbpMailKitOptions> abpMailKitConfiguration) |
||||
|
: base(smtpConfiguration, backgroundJobManager) |
||||
|
{ |
||||
|
AbpMailKitOptions = abpMailKitConfiguration.Value; |
||||
|
SmtpConfiguration = smtpConfiguration; |
||||
|
} |
||||
|
|
||||
|
protected override async Task SendEmailAsync(MailMessage mail) |
||||
|
{ |
||||
|
using (var client = await BuildClientAsync()) |
||||
|
{ |
||||
|
var message = MimeMessage.CreateFromMailMessage(mail); |
||||
|
await client.SendAsync(message); |
||||
|
await client.DisconnectAsync(true); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected override void SendEmail(MailMessage mail) |
||||
|
{ |
||||
|
using (var client = AsyncHelper.RunSync(BuildClientAsync)) |
||||
|
{ |
||||
|
var message = MimeMessage.CreateFromMailMessage(mail); |
||||
|
client.Send(message); |
||||
|
client.Disconnect(true); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public async Task<SmtpClient> BuildClientAsync() |
||||
|
{ |
||||
|
var client = new SmtpClient(); |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
await ConfigureClient(client); |
||||
|
return client; |
||||
|
} |
||||
|
catch |
||||
|
{ |
||||
|
client.Dispose(); |
||||
|
throw; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected virtual async Task ConfigureClient(SmtpClient client) |
||||
|
{ |
||||
|
client.Connect( |
||||
|
await SmtpConfiguration.GetHostAsync(), |
||||
|
await SmtpConfiguration.GetPortAsync(), |
||||
|
await GetSecureSocketOption() |
||||
|
); |
||||
|
|
||||
|
if (await SmtpConfiguration.GetUseDefaultCredentialsAsync()) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
client.Authenticate( |
||||
|
await SmtpConfiguration.GetUserNameAsync(), |
||||
|
await SmtpConfiguration.GetPasswordAsync() |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
protected virtual async Task<SecureSocketOptions> GetSecureSocketOption() |
||||
|
{ |
||||
|
if (AbpMailKitOptions.SecureSocketOption.HasValue) |
||||
|
{ |
||||
|
return AbpMailKitOptions.SecureSocketOption.Value; |
||||
|
} |
||||
|
|
||||
|
return await SmtpConfiguration.GetEnableSslAsync() |
||||
|
? SecureSocketOptions.SslOnConnect |
||||
|
: SecureSocketOptions.StartTlsWhenAvailable; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.Json |
||||
|
{ |
||||
|
[Route("api/json-result-test")] |
||||
|
public class JsonResultController : AbpController |
||||
|
{ |
||||
|
[HttpGet] |
||||
|
[Route("json-result-action")] |
||||
|
public Task<JsonResultModel> ObjectResultAction() |
||||
|
{ |
||||
|
return Task.FromResult(new JsonResultModel |
||||
|
{ |
||||
|
Time = DateTime.Parse("2019-01-01 11:59:59") |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public class JsonResultModel |
||||
|
{ |
||||
|
public DateTime Time { get; set; } |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Hosting; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Json; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.Json |
||||
|
{ |
||||
|
public class JsonResultController_Tests : AspNetCoreMvcTestBase |
||||
|
{ |
||||
|
protected override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services) |
||||
|
{ |
||||
|
services.Configure<AbpJsonOptions>(options => |
||||
|
{ |
||||
|
options.SerializerSettings.DateFormatString = "yyyy*MM*dd"; |
||||
|
}); |
||||
|
|
||||
|
base.ConfigureServices(context, services); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task DateFormatString_Test() |
||||
|
{ |
||||
|
var time = await GetResponseAsStringAsync( |
||||
|
"/api/json-result-test/json-result-action" |
||||
|
); |
||||
|
|
||||
|
time.ShouldContain("2019*01*01"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,40 @@ |
|||||
|
using System; |
||||
|
using Microsoft.AspNetCore.Hosting; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Json; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.Json |
||||
|
{ |
||||
|
public class JsonSerializer_Tests : AspNetCoreMvcTestBase |
||||
|
{ |
||||
|
private readonly IJsonSerializer _jsonSerializer; |
||||
|
|
||||
|
public JsonSerializer_Tests() |
||||
|
{ |
||||
|
_jsonSerializer = ServiceProvider.GetRequiredService<IJsonSerializer>(); |
||||
|
} |
||||
|
|
||||
|
protected override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services) |
||||
|
{ |
||||
|
services.Configure<AbpJsonOptions>(options => |
||||
|
{ |
||||
|
options.SerializerSettings.DateFormatString = "yyyy*MM*dd"; |
||||
|
}); |
||||
|
|
||||
|
base.ConfigureServices(context, services); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void DateFormatString_Test() |
||||
|
{ |
||||
|
var output = _jsonSerializer.Serialize(new |
||||
|
{ |
||||
|
Time = DateTime.Parse("2019-01-01 11:59:59") |
||||
|
}); |
||||
|
|
||||
|
output.ShouldContain("2019*01*01"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp2.2</TargetFramework> |
||||
|
<LangVersion>latest</LangVersion> |
||||
|
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> |
||||
|
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
||||
|
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
||||
|
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.MailKit\Volo.Abp.MailKit.csproj" /> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> |
||||
|
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,13 @@ |
|||||
|
using Volo.Abp.Autofac; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp.MailKit |
||||
|
{ |
||||
|
[DependsOn( |
||||
|
typeof(AbpMailKitModule), |
||||
|
typeof(AbpAutofacModule), |
||||
|
typeof(AbpTestBaseModule))] |
||||
|
public class AbpMailKitTestModule :AbpModule |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,48 @@ |
|||||
|
using System.Net.Mail; |
||||
|
using System.Threading.Tasks; |
||||
|
using NSubstitute; |
||||
|
using Volo.Abp.BackgroundJobs; |
||||
|
using Volo.Abp.Emailing.Smtp; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.MailKit |
||||
|
{ |
||||
|
//Tests are commented because those tests can pass only when a true email configuration is set.
|
||||
|
public class MailKitSmtpEmailSender_Tests : AbpIntegratedTest<AbpMailKitTestModule> |
||||
|
{ |
||||
|
//[Fact]
|
||||
|
public async Task ShouldSendMailMessageAsync() |
||||
|
{ |
||||
|
var mailSender = CreateMailKitEmailSender(); |
||||
|
var mailMessage = new MailMessage("from_mail_address@asd.com", "to_mail_address@asd.com", "subject", "body") |
||||
|
{ IsBodyHtml = true }; |
||||
|
|
||||
|
await mailSender.SendAsync(mailMessage); |
||||
|
} |
||||
|
|
||||
|
//[Fact]
|
||||
|
public void ShouldSendMailMessage() |
||||
|
{ |
||||
|
var mailSender = CreateMailKitEmailSender(); |
||||
|
var mailMessage = new MailMessage("from_mail_address@asd.com", "to_mail_address@asd.com", "subject", "body") |
||||
|
{ IsBodyHtml = true }; |
||||
|
|
||||
|
mailSender.Send(mailMessage); |
||||
|
} |
||||
|
|
||||
|
private static MailKitSmtpEmailSender CreateMailKitEmailSender() |
||||
|
{ |
||||
|
var mailConfig = Substitute.For<ISmtpEmailSenderConfiguration>(); |
||||
|
var bgJob = Substitute.For<IBackgroundJobManager>(); |
||||
|
|
||||
|
mailConfig.GetHostAsync().Returns(Task.FromResult("stmp_server_name")); |
||||
|
mailConfig.GetUserNameAsync().Returns(Task.FromResult("mail_server_user_name")); |
||||
|
mailConfig.GetPasswordAsync().Returns(Task.FromResult("mail_server_password")); |
||||
|
mailConfig.GetPortAsync().Returns(Task.FromResult(587)); |
||||
|
mailConfig.GetEnableSslAsync().Returns(Task.FromResult(false)); |
||||
|
|
||||
|
var mailSender = new MailKitSmtpEmailSender(mailConfig, bgJob, null); |
||||
|
return mailSender; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue