mirror of https://github.com/abpframework/abp.git
7 changed files with 174 additions and 2 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,9 @@ |
|||
using MailKit.Security; |
|||
|
|||
namespace Volo.Abp.MailKit |
|||
{ |
|||
public class AbpMailKitConfiguration : IAbpMailKitConfiguration |
|||
{ |
|||
public SecureSocketOptions? SecureSocketOption { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
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 interface IAbpMailKitConfiguration |
|||
{ |
|||
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,97 @@ |
|||
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 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 IAbpMailKitConfiguration AbpMailKitConfiguration { get; } |
|||
|
|||
protected ISmtpEmailSenderConfiguration SmtpConfiguration { get; } |
|||
|
|||
public MailKitSmtpEmailSender(ISmtpEmailSenderConfiguration smtpConfiguration, |
|||
IBackgroundJobManager backgroundJobManager, |
|||
IAbpMailKitConfiguration abpMailKitConfiguration) |
|||
: base(smtpConfiguration, backgroundJobManager) |
|||
{ |
|||
AbpMailKitConfiguration = abpMailKitConfiguration; |
|||
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 (AbpMailKitConfiguration.SecureSocketOption.HasValue) |
|||
{ |
|||
return AbpMailKitConfiguration.SecureSocketOption.Value; |
|||
} |
|||
|
|||
return await SmtpConfiguration.GetEnableSslAsync() |
|||
? SecureSocketOptions.SslOnConnect |
|||
: SecureSocketOptions.StartTlsWhenAvailable; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue