This commit is contained in:
@@ -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())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user