[#15] MailWindow: Add email sending feature

This commit is contained in:
2024-03-05 16:32:21 +01:00
parent 0812c6a8f9
commit 74da1ba46f
8 changed files with 137 additions and 37 deletions

View File

@ -3,15 +3,16 @@ using Elwig.Helpers;
using Elwig.Helpers.Billing;
using Elwig.Models.Dtos;
using Elwig.Models.Entities;
using MailKit.Net.Smtp;
using Microsoft.EntityFrameworkCore;
using Microsoft.Win32;
using MimeKit;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
@ -543,8 +544,8 @@ namespace Elwig.Windows {
GenerateButton.IsEnabled = true;
Mouse.OverrideCursor = null;
PreviewButton.IsEnabled = true;
PrintButton.IsEnabled = true;
//EmailButton.IsEnabled = true;
PrintButton.IsEnabled = PrintDocument != null;
EmailButton.IsEnabled = EmailDocuments != null && App.Config.Smtp != null;
}
private void PreviewButton_Click(object sender, RoutedEventArgs evt) {
@ -560,7 +561,7 @@ namespace Elwig.Windows {
Directory.CreateDirectory(folder);
foreach (var item in docs.Select((d, i) => new { Index = i, Doc = d })) {
var doc = item.Doc;
var name = Regex.Replace(doc.Title.Replace('/', '-'), @"[^A-Za-z0-9ÄÜÖẞäöüß-]+", "_");
var name = Utils.NormalizeFileName(doc.Title);
doc.SaveTo($"{folder}/{item.Index + 1:00}.{name}.pdf");
}
@ -573,20 +574,65 @@ namespace Elwig.Windows {
private async void PrintButton_Click(object sender, RoutedEventArgs evt) {
if (PrintDocument == null) return;
PrintButton.IsEnabled = false;
var res = MessageBox.Show($"Sollen {PrintDocument.Pages} Blätter ({PrintDocument.TotalPages} Seiten) gedruckt werden?\n" +
$"Sind die \"Duplex-Einstellungen\" des Standarddruckers entsprechend eingestellt (doppelseitig bzw. einseitig)?",
"Rundschreiben drucken", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
if (res == MessageBoxResult.Yes) {
Mouse.OverrideCursor = Cursors.AppStarting;
if (App.Config.Debug) {
PrintDocument.Show();
} else {
await PrintDocument.Print();
}
Mouse.OverrideCursor = null;
}
PrintButton.IsEnabled = true;
}
private void EmailButton_Click(object sender, RoutedEventArgs evt) {
// TODO
private async void EmailButton_Click(object sender, RoutedEventArgs evt) {
if (App.Config.Smtp == null || EmailDocuments == null) return;
EmailButton.IsEnabled = false;
SmtpClient? client = null;
try {
Mouse.OverrideCursor = Cursors.AppStarting;
client = await Utils.GetSmtpClient();
Mouse.OverrideCursor = null;
var res = MessageBox.Show($"Sollen {EmailDocuments.Count} E-Mails verschickt werden?",
"Rundschreiben verschicken", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
if (res != MessageBoxResult.Yes) {
return;
}
Mouse.OverrideCursor = Cursors.AppStarting;
var subject = EmailSubjectInput.Text;
var text = EmailBodyInput.Text;
foreach (var (m, docs) in EmailDocuments) {
using var msg = new MimeMessage();
msg.From.Add(new MailboxAddress(App.Client.NameFull, App.Config.Smtp.Value.From));
msg.To.AddRange(m.EmailAddresses.OrderBy(a => a.Nr).Select(a => new MailboxAddress(m.AdministrativeName, a.Address)));
msg.Subject = subject;
var body = new Multipart("mixed") {
new TextPart("plain") { Text = text }
};
foreach (var doc in docs) {
var name = Utils.NormalizeFileName(doc.Title);
body.Add(doc.AsEmailAttachment($"{name}.pdf"));
}
msg.Body = body;
await client!.SendAsync(msg);
}
} catch (Exception exc) {
MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
} finally {
if (client != null)
await client.DisconnectAsync(true);
client?.Dispose();
EmailButton.IsEnabled = true;
Mouse.OverrideCursor = null;
}
}
public void AddDeliveryConfirmation() {