mirror of https://github.com/abpframework/abp.git
committed by
GitHub
13 changed files with 256 additions and 7 deletions
@ -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,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