logic added

This commit is contained in:
2021-02-04 22:45:41 +01:00
parent e5098c02d9
commit bf57e3a983
11 changed files with 150 additions and 28 deletions

View File

@@ -39,6 +39,34 @@ namespace Massmailer.Shared.Logic
return message; 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) public List<MimeMessage> CreateMessages(List<string> recipients, string subject, string body)
{ {
var result = new List<MimeMessage>(); var result = new List<MimeMessage>();

View 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;
}
}
}

View 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;
}
}
}

View File

@@ -6,6 +6,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="MailKit" Version="2.10.1" /> <PackageReference Include="MailKit" Version="2.10.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="PubSub" Version="4.0.1" /> <PackageReference Include="PubSub" Version="4.0.1" />
</ItemGroup> </ItemGroup>

View File

@@ -1,13 +0,0 @@
using System.Collections.Generic;
namespace Massmailer.Shared.Model
{
public class MassmailProject
{
public List<MassmailRecipient> Recipient { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
}

View File

@@ -0,0 +1,18 @@
using System.Collections.Generic;
namespace Massmailer.Shared.Model
{
public class MassmailerProject
{
public MassmailerProject()
{
this.Recipients = new List<MassmailerRecipient>();
}
public List<MassmailerRecipient> Recipients { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
}

View File

@@ -2,9 +2,9 @@
namespace Massmailer.Shared.Model 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; } public bool IsSent { get; set; }

View File

@@ -1,10 +1,4 @@
using System; namespace Massmailer.Shared.Model
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Massmailer.Shared.Model
{ {
public class SmtpSettings public class SmtpSettings
{ {

View File

@@ -0,0 +1,9 @@
namespace Massmailer.UI
{
public class Constants
{
public const string SmtpSettingsFileName = "smtpsettings.json";
public const string ProjectFileExtension = "mailer";
}
}

View File

@@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Massmailer.UI" xmlns:local="clr-namespace:Massmailer.UI"
mc:Ignorable="d" mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"> Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid> <Grid>
<Button x:Name="BtnSend" Click="Button_Click" Content="Send"/> <Button x:Name="BtnSend" Click="Button_Click" Content="Send"/>
</Grid> </Grid>

View File

@@ -4,6 +4,7 @@ using Massmailer.Shared.Model;
using PubSub; using PubSub;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -15,16 +16,15 @@ using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Massmailer.UI namespace Massmailer.UI
{ {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainView : Window public partial class MainView : Window
{ {
private readonly Hub hub = new Hub(); private readonly Hub hub = new Hub();
private SmtpSettings smtpSettings;
private MassmailerProject project;
private string selectedFilePath = string.Empty;
public MainView() public MainView()
{ {
@@ -60,12 +60,41 @@ namespace Massmailer.UI
"10@test.local" "10@test.local"
}; };
await mailer.SendMailsAsync(recipients, "Hello World Subject", "Hello World Body"); var project = new MassmailerProject();
project.Recipients.Add(new MassmailerRecipient { Address = "1@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "2@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "3@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "4@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "5@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "6@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "7@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "8@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "9@test.local" });
project.Recipients.Add(new MassmailerRecipient { Address = "10@test.local" });
project.Subject = "Hello World Subject";
project.Body = "Hello World Body";
await mailer.SendMailsAsync(project);
var result = "";
foreach (var recipient in project.Recipients)
{
result += $"{recipient.Address} - {recipient.IsSent} - {recipient.SentDate}\r\n";
}
MassmailerProjectLogic.Save(project, "project.json");
MessageBox.Show(result);
} }
private void MailSentEventHandler(MailSentEvent mailSentEvent) private void MailSentEventHandler(MailSentEvent mailSentEvent)
{ {
this.BtnSend.Content = $"mail sent: {mailSentEvent.Recipient} at {mailSentEvent.Timestamp}"; this.BtnSend.Content = $"mail sent: {mailSentEvent.Recipient} at {mailSentEvent.Timestamp}";
} }
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
this.smtpSettings = await SmtpSettingsLogic.Load(Path.Combine(Environment.CurrentDirectory, Constants.SmtpSettingsFileName));
}
} }
} }