init
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Mail;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Kreta.Core;
|
||||
using Kreta.Job.Tasks.Core;
|
||||
using Kreta.Job.Tasks.Core.Models;
|
||||
|
||||
namespace Kreta.Job.Tasks
|
||||
{
|
||||
public class EmailJob : IEmailJob
|
||||
{
|
||||
private KretaJobConfig KretaJobConfig { get; }
|
||||
|
||||
public EmailJob(KretaJobConfig kretaJobConfig)
|
||||
{
|
||||
KretaJobConfig = kretaJobConfig;
|
||||
}
|
||||
|
||||
public async Task SendMailAsync(EmailModel emailModel)
|
||||
{
|
||||
if (KretaJobConfig.IsEmailEnabled)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(emailModel.TargetEmail))
|
||||
{
|
||||
emailModel.TargetEmail = KretaJobConfig.DefaultEmail;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(emailModel.FromEmail))
|
||||
{
|
||||
emailModel.FromEmail = KretaJobConfig.DefaultEmail;
|
||||
}
|
||||
|
||||
if (emailModel.TargetEmail.IsValidEmail())
|
||||
{
|
||||
var mail = new MailMessage(emailModel.FromEmail, emailModel.TargetEmail)
|
||||
{
|
||||
Subject = emailModel.Subject,
|
||||
SubjectEncoding = Encoding.UTF8,
|
||||
Body = $"{emailModel.Message}{emailModel.Footer}",
|
||||
IsBodyHtml = true,
|
||||
BodyEncoding = Encoding.UTF8,
|
||||
DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
|
||||
};
|
||||
|
||||
if (emailModel.Buffer != null && emailModel.Buffer.Length > 0 && !string.IsNullOrWhiteSpace(emailModel.FileName))
|
||||
{
|
||||
MemoryStream ms = new MemoryStream(emailModel.Buffer);
|
||||
mail.Attachments.Add(new Attachment(ms, emailModel.FileName));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(emailModel.Bcc))
|
||||
mail.Bcc.Add(emailModel.Bcc);
|
||||
|
||||
using (var client = new SmtpClient())
|
||||
{
|
||||
await client.SendMailAsync(mail);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SendMailMessages(List<EmailModel> emailJobList)
|
||||
{
|
||||
if (KretaJobConfig.IsEmailEnabled)
|
||||
{
|
||||
foreach (var emailModel in emailJobList)
|
||||
{
|
||||
|
||||
if (string.IsNullOrWhiteSpace(emailModel.TargetEmail))
|
||||
{
|
||||
emailModel.TargetEmail = KretaJobConfig.DefaultEmail;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(emailModel.FromEmail))
|
||||
{
|
||||
emailModel.FromEmail = KretaJobConfig.DefaultEmail;
|
||||
}
|
||||
|
||||
if (emailModel.TargetEmail.IsValidEmail())
|
||||
{
|
||||
var mail = new MailMessage(emailModel.FromEmail, emailModel.TargetEmail)
|
||||
{
|
||||
Subject = emailModel.Subject,
|
||||
SubjectEncoding = Encoding.UTF8,
|
||||
Body = $"{emailModel.Message}{emailModel.Footer}",
|
||||
IsBodyHtml = true,
|
||||
BodyEncoding = Encoding.UTF8,
|
||||
DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
|
||||
};
|
||||
|
||||
if (emailModel.Buffer != null && emailModel.Buffer.Length > 0 && !string.IsNullOrWhiteSpace(emailModel.FileName))
|
||||
{
|
||||
MemoryStream ms = new MemoryStream(emailModel.Buffer);
|
||||
mail.Attachments.Add(new Attachment(ms, emailModel.FileName));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(emailModel.Bcc))
|
||||
mail.Bcc.Add(emailModel.Bcc);
|
||||
|
||||
using (var client = new SmtpClient())
|
||||
{
|
||||
client.Send(mail);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user