diff --git a/Massmailer.Shared/Logic/Mailer.cs b/Massmailer.Shared/Logic/Mailer.cs index e72bb59..3e218da 100644 --- a/Massmailer.Shared/Logic/Mailer.cs +++ b/Massmailer.Shared/Logic/Mailer.cs @@ -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(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 CreateMessages(List recipients, string subject, string body) { var result = new List(); diff --git a/Massmailer.Shared/Logic/MassmailerProjectLogic.cs b/Massmailer.Shared/Logic/MassmailerProjectLogic.cs new file mode 100644 index 0000000..fd4e4d3 --- /dev/null +++ b/Massmailer.Shared/Logic/MassmailerProjectLogic.cs @@ -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 Load(string filePath) + { + var project = new MassmailerProject(); + if (File.Exists(filePath)) + { + var json = await File.ReadAllTextAsync(filePath); + project = JsonConvert.DeserializeObject(json); + } + + return project; + } + } +} diff --git a/Massmailer.Shared/Logic/SmtpSettingsLogic.cs b/Massmailer.Shared/Logic/SmtpSettingsLogic.cs new file mode 100644 index 0000000..4a3a76a --- /dev/null +++ b/Massmailer.Shared/Logic/SmtpSettingsLogic.cs @@ -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 Load(string filePath) + { + var project = new SmtpSettings(); + if (File.Exists(filePath)) + { + var json = await File.ReadAllTextAsync(filePath); + project = JsonConvert.DeserializeObject(json); + } + + return project; + } + } +} diff --git a/Massmailer.Shared/Massmailer.Shared.csproj b/Massmailer.Shared/Massmailer.Shared.csproj index a043f07..d62e25f 100644 --- a/Massmailer.Shared/Massmailer.Shared.csproj +++ b/Massmailer.Shared/Massmailer.Shared.csproj @@ -6,6 +6,7 @@ + diff --git a/Massmailer.Shared/Model/MassmailProject.cs b/Massmailer.Shared/Model/MassmailProject.cs deleted file mode 100644 index 95d699e..0000000 --- a/Massmailer.Shared/Model/MassmailProject.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Collections.Generic; - -namespace Massmailer.Shared.Model -{ - public class MassmailProject - { - public List Recipient { get; set; } - - public string Subject { get; set; } - - public string Body { get; set; } - } -} diff --git a/Massmailer.Shared/Model/MassmailerProject.cs b/Massmailer.Shared/Model/MassmailerProject.cs new file mode 100644 index 0000000..1532521 --- /dev/null +++ b/Massmailer.Shared/Model/MassmailerProject.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; + +namespace Massmailer.Shared.Model +{ + public class MassmailerProject + { + public MassmailerProject() + { + this.Recipients = new List(); + } + + public List Recipients { get; set; } + + public string Subject { get; set; } + + public string Body { get; set; } + } +} diff --git a/Massmailer.Shared/Model/MassmailRecipient.cs b/Massmailer.Shared/Model/MassmailerRecipient.cs similarity index 66% rename from Massmailer.Shared/Model/MassmailRecipient.cs rename to Massmailer.Shared/Model/MassmailerRecipient.cs index b15bf44..bece288 100644 --- a/Massmailer.Shared/Model/MassmailRecipient.cs +++ b/Massmailer.Shared/Model/MassmailerRecipient.cs @@ -2,9 +2,9 @@ namespace Massmailer.Shared.Model { - public class MassmailRecipient + public class MassmailerRecipient { - public string Recipient { get; set; } + public string Address { get; set; } public bool IsSent { get; set; } diff --git a/Massmailer.Shared/Model/SmtpSettings.cs b/Massmailer.Shared/Model/SmtpSettings.cs index d094021..b12e89a 100644 --- a/Massmailer.Shared/Model/SmtpSettings.cs +++ b/Massmailer.Shared/Model/SmtpSettings.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Massmailer.Shared.Model +namespace Massmailer.Shared.Model { public class SmtpSettings { diff --git a/Massmailer.UI/Constants.cs b/Massmailer.UI/Constants.cs new file mode 100644 index 0000000..af168f2 --- /dev/null +++ b/Massmailer.UI/Constants.cs @@ -0,0 +1,9 @@ +namespace Massmailer.UI +{ + public class Constants + { + public const string SmtpSettingsFileName = "smtpsettings.json"; + + public const string ProjectFileExtension = "mailer"; + } +} diff --git a/Massmailer.UI/Views/MainView.xaml b/Massmailer.UI/Views/MainView.xaml index de66884..7836fe0 100644 --- a/Massmailer.UI/Views/MainView.xaml +++ b/Massmailer.UI/Views/MainView.xaml @@ -5,7 +5,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Massmailer.UI" mc:Ignorable="d" - Title="MainWindow" Height="450" Width="800"> + Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">