This commit is contained in:
56
server/AyaNova/util/Mailer.cs
Normal file
56
server/AyaNova/util/Mailer.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using MailKit.Net.Smtp;
|
||||
using MimeKit;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using MimeKit.Text;
|
||||
|
||||
namespace AyaNova.Util
|
||||
{
|
||||
public interface IMailer
|
||||
{
|
||||
Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string bodyHTML = null);
|
||||
}
|
||||
|
||||
public class Mailer : IMailer
|
||||
{
|
||||
public Mailer()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public async Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string bodyHTML = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var message = new MimeMessage();
|
||||
message.From.Add(new MailboxAddress(smtpSettings.NotifyFromAddress, smtpSettings.NotifyFromAddress));
|
||||
message.To.Add(MailboxAddress.Parse(email));
|
||||
message.Subject = subject;
|
||||
if (!string.IsNullOrWhiteSpace(body))
|
||||
message.Body = new TextPart(TextFormat.Plain) { Text = body };
|
||||
if (!string.IsNullOrWhiteSpace(bodyHTML))
|
||||
message.Body = new TextPart("html")
|
||||
{
|
||||
Text = body
|
||||
};
|
||||
|
||||
using (var client = new SmtpClient())
|
||||
{
|
||||
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
||||
|
||||
bool UseSSL = smtpSettings.ConnectionSecurity != Biz.NotifyMailSecurity.None;
|
||||
await client.ConnectAsync(smtpSettings.SmtpServerAddress, smtpSettings.SmtpServerPort, UseSSL);
|
||||
|
||||
|
||||
await client.AuthenticateAsync(smtpSettings.SmtpAccount, smtpSettings.SmtpPassword);
|
||||
await client.SendAsync(message);
|
||||
await client.DisconnectAsync(true);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new InvalidOperationException(e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user