mirror of https://github.com/abpframework/abp
parent
e3261f22ec
commit
c52d192374
@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Net.Mail;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Volo.Abp.Emailing
|
||||
{
|
||||
/// <summary>
|
||||
/// This class can be used as base to implement <see cref="IEmailSender"/>.
|
||||
/// </summary>
|
||||
public abstract class EmailSenderBase : IEmailSender
|
||||
{
|
||||
public IEmailSenderConfiguration Configuration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="configuration">Configuration</param>
|
||||
protected EmailSenderBase(IEmailSenderConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public virtual async Task SendAsync(string to, string subject, string body, bool isBodyHtml = true)
|
||||
{
|
||||
await SendAsync(new MailMessage
|
||||
{
|
||||
To = { to },
|
||||
Subject = subject,
|
||||
Body = body,
|
||||
IsBodyHtml = isBodyHtml
|
||||
});
|
||||
}
|
||||
|
||||
public virtual void Send(string to, string subject, string body, bool isBodyHtml = true)
|
||||
{
|
||||
Send(new MailMessage
|
||||
{
|
||||
To = { to },
|
||||
Subject = subject,
|
||||
Body = body,
|
||||
IsBodyHtml = isBodyHtml
|
||||
});
|
||||
}
|
||||
|
||||
public virtual async Task SendAsync(string from, string to, string subject, string body, bool isBodyHtml = true)
|
||||
{
|
||||
await SendAsync(new MailMessage(from, to, subject, body) { IsBodyHtml = isBodyHtml });
|
||||
}
|
||||
|
||||
public virtual void Send(string from, string to, string subject, string body, bool isBodyHtml = true)
|
||||
{
|
||||
Send(new MailMessage(from, to, subject, body) { IsBodyHtml = isBodyHtml });
|
||||
}
|
||||
|
||||
public virtual async Task SendAsync(MailMessage mail, bool normalize = true)
|
||||
{
|
||||
if (normalize)
|
||||
{
|
||||
NormalizeMail(mail);
|
||||
}
|
||||
|
||||
await SendEmailAsync(mail);
|
||||
}
|
||||
|
||||
public virtual void Send(MailMessage mail, bool normalize = true)
|
||||
{
|
||||
if (normalize)
|
||||
{
|
||||
NormalizeMail(mail);
|
||||
}
|
||||
|
||||
SendEmail(mail);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Should implement this method to send email in derived classes.
|
||||
/// </summary>
|
||||
/// <param name="mail">Mail to be sent</param>
|
||||
protected abstract Task SendEmailAsync(MailMessage mail);
|
||||
|
||||
/// <summary>
|
||||
/// Should implement this method to send email in derived classes.
|
||||
/// </summary>
|
||||
/// <param name="mail">Mail to be sent</param>
|
||||
protected abstract void SendEmail(MailMessage mail);
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes given email.
|
||||
/// Fills <see cref="MailMessage.From"/> if it's not filled before.
|
||||
/// Sets encodings to UTF8 if they are not set before.
|
||||
/// </summary>
|
||||
/// <param name="mail">Mail to be normalized</param>
|
||||
protected virtual void NormalizeMail(MailMessage mail)
|
||||
{
|
||||
if (mail.From == null || mail.From.Address.IsNullOrEmpty())
|
||||
{
|
||||
mail.From = new MailAddress(
|
||||
Configuration.DefaultFromAddress,
|
||||
Configuration.DefaultFromDisplayName,
|
||||
Encoding.UTF8
|
||||
);
|
||||
}
|
||||
|
||||
if (mail.HeadersEncoding == null)
|
||||
{
|
||||
mail.HeadersEncoding = Encoding.UTF8;
|
||||
}
|
||||
|
||||
if (mail.SubjectEncoding == null)
|
||||
{
|
||||
mail.SubjectEncoding = Encoding.UTF8;
|
||||
}
|
||||
|
||||
if (mail.BodyEncoding == null)
|
||||
{
|
||||
mail.BodyEncoding = Encoding.UTF8;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
using Volo.Abp.Settings;
|
||||
|
||||
namespace Volo.Abp.Emailing
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines settings to send emails.
|
||||
/// <see cref="EmailSettingNames"/> for all available configurations.
|
||||
/// </summary>
|
||||
internal class EmailSettingProvider : SettingDefinitionProvider
|
||||
{
|
||||
public override void Define(ISettingDefinitionContext context)
|
||||
{
|
||||
context.Add(
|
||||
new SettingDefinition(EmailSettingNames.Smtp.Host, "127.0.0.1"),
|
||||
new SettingDefinition(EmailSettingNames.Smtp.Port, "25"),
|
||||
new SettingDefinition(EmailSettingNames.Smtp.UserName),
|
||||
new SettingDefinition(EmailSettingNames.Smtp.Password),
|
||||
new SettingDefinition(EmailSettingNames.Smtp.Domain),
|
||||
new SettingDefinition(EmailSettingNames.Smtp.EnableSsl, "false"),
|
||||
new SettingDefinition(EmailSettingNames.Smtp.UseDefaultCredentials, "true"),
|
||||
new SettingDefinition(EmailSettingNames.DefaultFromAddress),
|
||||
new SettingDefinition(EmailSettingNames.DefaultFromDisplayName)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,49 @@
|
||||
using System.Net.Mail;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace Volo.Abp.Emailing
|
||||
{
|
||||
/// <summary>
|
||||
/// This class is an implementation of <see cref="IEmailSender"/> as similar to null pattern.
|
||||
/// It does not send emails but logs them.
|
||||
/// </summary>
|
||||
public class NullEmailSender : EmailSenderBase
|
||||
{
|
||||
public ILogger<NullEmailSender> Logger { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="NullEmailSender"/> object.
|
||||
/// </summary>
|
||||
/// <param name="configuration">Configuration</param>
|
||||
public NullEmailSender(IEmailSenderConfiguration configuration)
|
||||
: base(configuration)
|
||||
{
|
||||
Logger = NullLogger<NullEmailSender>.Instance;
|
||||
}
|
||||
|
||||
protected override Task SendEmailAsync(MailMessage mail)
|
||||
{
|
||||
Logger.LogWarning("USING NullEmailSender!");
|
||||
Logger.LogDebug("SendEmailAsync:");
|
||||
LogEmail(mail);
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
protected override void SendEmail(MailMessage mail)
|
||||
{
|
||||
Logger.LogWarning("USING NullEmailSender!");
|
||||
Logger.LogWarning("SendEmail:");
|
||||
LogEmail(mail);
|
||||
}
|
||||
|
||||
private void LogEmail(MailMessage mail)
|
||||
{
|
||||
Logger.LogDebug(mail.To.ToString());
|
||||
Logger.LogDebug(mail.CC.ToString());
|
||||
Logger.LogDebug(mail.Subject);
|
||||
Logger.LogDebug(mail.Body);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace Volo.Abp.Emailing.Smtp
|
||||
{
|
||||
/// <summary>
|
||||
/// Used to send emails over SMTP.
|
||||
/// </summary>
|
||||
public interface ISmtpEmailSender : IEmailSender
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates and configures new <see cref="SmtpClient"/> object to send emails.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// An <see cref="SmtpClient"/> object that is ready to send emails.
|
||||
/// </returns>
|
||||
SmtpClient BuildClient();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using System.Threading.Tasks;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
|
||||
namespace Volo.Abp.Emailing.Smtp
|
||||
{
|
||||
/// <summary>
|
||||
/// Used to send emails over SMTP.
|
||||
/// </summary>
|
||||
public class SmtpEmailSender : EmailSenderBase, ISmtpEmailSender, ITransientDependency
|
||||
{
|
||||
private readonly ISmtpEmailSenderConfiguration _configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="SmtpEmailSender"/>.
|
||||
/// </summary>
|
||||
/// <param name="configuration">Configuration</param>
|
||||
public SmtpEmailSender(ISmtpEmailSenderConfiguration configuration)
|
||||
: base(configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public SmtpClient BuildClient()
|
||||
{
|
||||
var host = _configuration.Host;
|
||||
var port = _configuration.Port;
|
||||
|
||||
var smtpClient = new SmtpClient(host, port);
|
||||
try
|
||||
{
|
||||
if (_configuration.EnableSsl)
|
||||
{
|
||||
smtpClient.EnableSsl = true;
|
||||
}
|
||||
|
||||
if (_configuration.UseDefaultCredentials)
|
||||
{
|
||||
smtpClient.UseDefaultCredentials = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
smtpClient.UseDefaultCredentials = false;
|
||||
|
||||
var userName = _configuration.UserName;
|
||||
if (!userName.IsNullOrEmpty())
|
||||
{
|
||||
var password = _configuration.Password;
|
||||
var domain = _configuration.Domain;
|
||||
smtpClient.Credentials = !domain.IsNullOrEmpty()
|
||||
? new NetworkCredential(userName, password, domain)
|
||||
: new NetworkCredential(userName, password);
|
||||
}
|
||||
}
|
||||
|
||||
return smtpClient;
|
||||
}
|
||||
catch
|
||||
{
|
||||
smtpClient.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
protected override async Task SendEmailAsync(MailMessage mail)
|
||||
{
|
||||
using (var smtpClient = BuildClient())
|
||||
{
|
||||
await smtpClient.SendMailAsync(mail);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void SendEmail(MailMessage mail)
|
||||
{
|
||||
using (var smtpClient = BuildClient())
|
||||
{
|
||||
smtpClient.Send(mail);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue