This commit is contained in:
2022-03-04 18:41:49 +00:00
parent c584e62e50
commit 1e7369382e
4 changed files with 103 additions and 7 deletions

View File

@@ -3,12 +3,13 @@ using MimeKit;
using System;
using System.Threading.Tasks;
using MimeKit.Text;
using System.IO;
namespace AyaNova.Util
{
public interface IMailer
{
Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string bodyHTML = null);
Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string attachPDF = null);
}
public class Mailer : IMailer
@@ -18,7 +19,7 @@ namespace AyaNova.Util
}
public async Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string bodyHTML = null)
public async Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string attachPDFPath = null)
{
try
{
@@ -26,13 +27,28 @@ namespace AyaNova.Util
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")
if (!string.IsNullOrWhiteSpace(attachPDFPath))
{
var attachment = new MimePart("application/pdf", "pdf")
{
Text = body
Content = new MimeContent(File.OpenRead(attachPDFPath), ContentEncoding.Default),
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
FileName = Path.GetFileName(attachPDFPath)
};
var multipart = new Multipart("mixed");
if (!string.IsNullOrWhiteSpace(body))
multipart.Add(new TextPart(TextFormat.Plain) { Text = body });
multipart.Add(attachment);
message.Body = multipart;
}
else
{
if (!string.IsNullOrWhiteSpace(body))
message.Body = new TextPart(TextFormat.Plain) { Text = body };
}
using (var client = new SmtpClient())
{