161 lines
5.5 KiB
C#
161 lines
5.5 KiB
C#
namespace PendingEmails
|
|
{
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
public static partial class Program
|
|
{
|
|
public class AppSettings
|
|
{
|
|
public static string LogFolder { get; private set; }
|
|
|
|
public static string MSSQLConnectionString { get; private set; }
|
|
|
|
public static string Sender { get; private set; }
|
|
|
|
public static string ExcludeRecipients { get; private set; }
|
|
|
|
public static string SMTPHost { get; private set; }
|
|
|
|
public static int SMTPPort { get; private set; }
|
|
|
|
public static string SMTPUsername { get; private set; }
|
|
|
|
public static string SMTPPassword { get; private set; }
|
|
|
|
public static int Interval { get; private set; }
|
|
|
|
public static bool TryLoadExeConfiguration(out ICollection<string> errors)
|
|
{
|
|
errors = new HashSet<string>();
|
|
|
|
var exeFileInfo = new FileInfo(typeof(Program).Assembly.Location);
|
|
var settings = ConfigurationManager
|
|
.OpenExeConfiguration(exeFileInfo.FullName)
|
|
?.AppSettings
|
|
?.Settings;
|
|
var loaded = true;
|
|
|
|
if (settings is null)
|
|
{
|
|
errors.Add($"{exeFileInfo.Name} nicht gefunden.");
|
|
|
|
loaded = false;
|
|
}
|
|
else
|
|
{
|
|
var keys = settings.AllKeys;
|
|
|
|
LogFolder = exeFileInfo.Directory.FullName;
|
|
|
|
if (!keys.Any(x => x == nameof(LogFolder)))
|
|
{
|
|
errors.Add($"{nameof(LogFolder)} nicht definiert in die {exeFileInfo.Name}.");
|
|
errors.Add($"{LogFolder} wird benutzt.");
|
|
}
|
|
else
|
|
{
|
|
LogFolder = settings[nameof(LogFolder)]?.Value;
|
|
}
|
|
|
|
if (!keys.Any(x => x == nameof(MSSQLConnectionString)))
|
|
{
|
|
errors.Add($"{nameof(MSSQLConnectionString)} nicht definiert in die {exeFileInfo.Name}.");
|
|
|
|
loaded = false;
|
|
}
|
|
else
|
|
{
|
|
MSSQLConnectionString = settings[nameof(MSSQLConnectionString)]?.Value;
|
|
}
|
|
|
|
if (!keys.Any(x => x == nameof(Sender)))
|
|
{
|
|
errors.Add($"{nameof(Sender)} nicht definiert in die {exeFileInfo.Name}.");
|
|
|
|
loaded = false;
|
|
}
|
|
else
|
|
{
|
|
Sender = settings[nameof(Sender)]?.Value;
|
|
}
|
|
|
|
if (!keys.Any(x => x == nameof(ExcludeRecipients)))
|
|
{
|
|
errors.Add($"{nameof(ExcludeRecipients)} nicht definiert in die {exeFileInfo.Name}.");
|
|
}
|
|
else
|
|
{
|
|
ExcludeRecipients = settings[nameof(ExcludeRecipients)]?.Value;
|
|
}
|
|
|
|
if (!keys.Any(x => x == nameof(SMTPHost)))
|
|
{
|
|
errors.Add($"{nameof(SMTPHost)} nicht definiert in die {exeFileInfo.Name}.");
|
|
|
|
loaded = false;
|
|
}
|
|
else
|
|
{
|
|
SMTPHost = settings[nameof(SMTPHost)]?.Value;
|
|
}
|
|
|
|
if (!keys.Any(x => x == nameof(SMTPPort)))
|
|
{
|
|
errors.Add($"{nameof(SMTPPort)} nicht definiert in die {exeFileInfo.Name}.");
|
|
|
|
loaded = false;
|
|
}
|
|
else if (!int.TryParse($"{settings[nameof(SMTPPort)]?.Value}", out var smtpPort))
|
|
{
|
|
errors.Add($"{nameof(SMTPPort)} ist ungültig.");
|
|
|
|
loaded = false;
|
|
}
|
|
else
|
|
{
|
|
SMTPPort = smtpPort;
|
|
}
|
|
|
|
if (!keys.Any(x => x == nameof(SMTPUsername)))
|
|
{
|
|
errors.Add($"{nameof(SMTPUsername)} nicht definiert in die {exeFileInfo.Name}.");
|
|
}
|
|
else
|
|
{
|
|
SMTPUsername = settings[nameof(SMTPUsername)]?.Value;
|
|
}
|
|
|
|
if (!keys.Any(x => x == nameof(SMTPPassword)))
|
|
{
|
|
errors.Add($"{nameof(SMTPPassword)} nicht definiert in die {exeFileInfo.Name}.");
|
|
}
|
|
else
|
|
{
|
|
SMTPPassword = settings[nameof(SMTPPassword)]?.Value;
|
|
}
|
|
|
|
Interval = 600_000;
|
|
|
|
if (!keys.Any(x => x == nameof(Interval)))
|
|
{
|
|
errors.Add($"{nameof(Interval)} nicht definiert in die {exeFileInfo.Name}.");
|
|
}
|
|
else if (!int.TryParse($"{settings[nameof(Interval)]?.Value}", out var interval))
|
|
{
|
|
errors.Add($"{nameof(Interval)} ist ungültig.");
|
|
}
|
|
else
|
|
{
|
|
Interval = interval;
|
|
}
|
|
}
|
|
|
|
return loaded;
|
|
}
|
|
}
|
|
}
|
|
}
|