logic added
This commit is contained in:
@@ -39,6 +39,34 @@ namespace Massmailer.Shared.Logic
|
||||
return message;
|
||||
}
|
||||
|
||||
public async Task SendMailsAsync(MassmailerProject project)
|
||||
{
|
||||
try
|
||||
{
|
||||
await this.smtpClient.ConnectAsync(this.smtpSettings.Server, this.smtpSettings.Port, this.smtpSettings.UseSsl);
|
||||
await this.smtpClient.AuthenticateAsync(this.smtpSettings.Username, this.smtpSettings.Password);
|
||||
|
||||
foreach (var recipient in project.Recipients)
|
||||
{
|
||||
await this.CheckForWaitTime();
|
||||
|
||||
var message = this.CreateMessage(recipient.Address, project.Subject, project.Body);
|
||||
await this.smtpClient.SendAsync(message);
|
||||
|
||||
this.lastMailSent = DateTime.Now;
|
||||
recipient.IsSent = true;
|
||||
recipient.SentDate = this.lastMailSent;
|
||||
this.hub.Publish<MailSentEvent>(new MailSentEvent(message.To[0].ToString(), this.lastMailSent));
|
||||
}
|
||||
|
||||
await this.smtpClient.DisconnectAsync(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException("Something went wrong while sending many mails", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public List<MimeMessage> CreateMessages(List<string> recipients, string subject, string body)
|
||||
{
|
||||
var result = new List<MimeMessage>();
|
||||
|
||||
28
Massmailer.Shared/Logic/MassmailerProjectLogic.cs
Normal file
28
Massmailer.Shared/Logic/MassmailerProjectLogic.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using Massmailer.Shared.Model;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Massmailer.Shared.Logic
|
||||
{
|
||||
public static class MassmailerProjectLogic
|
||||
{
|
||||
public async static void Save(MassmailerProject project, string filePath)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(project);
|
||||
await File.WriteAllTextAsync(filePath, json);
|
||||
}
|
||||
|
||||
public async static Task<MassmailerProject> Load(string filePath)
|
||||
{
|
||||
var project = new MassmailerProject();
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
var json = await File.ReadAllTextAsync(filePath);
|
||||
project = JsonConvert.DeserializeObject<MassmailerProject>(json);
|
||||
}
|
||||
|
||||
return project;
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Massmailer.Shared/Logic/SmtpSettingsLogic.cs
Normal file
28
Massmailer.Shared/Logic/SmtpSettingsLogic.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using Massmailer.Shared.Model;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Massmailer.Shared.Logic
|
||||
{
|
||||
public static class SmtpSettingsLogic
|
||||
{
|
||||
public async static void Save(SmtpSettings project, string filePath)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(project);
|
||||
await File.WriteAllTextAsync(filePath, json);
|
||||
}
|
||||
|
||||
public async static Task<SmtpSettings> Load(string filePath)
|
||||
{
|
||||
var project = new SmtpSettings();
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
var json = await File.ReadAllTextAsync(filePath);
|
||||
project = JsonConvert.DeserializeObject<SmtpSettings>(json);
|
||||
}
|
||||
|
||||
return project;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user