| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System.Net;
- using System.Net.Mail;
- using System.Threading.Tasks;
- using Abp.Dependency;
- using Abp.Extensions;
- namespace Abp.Net.Mail.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);
- }
- }
- }
- }
|