Add SMTP support

This commit is contained in:
jvyden 2022-02-16 18:53:33 -05:00
commit 3bba7eff1b
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
3 changed files with 69 additions and 1 deletions

View file

@ -88,6 +88,8 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=PS/@EntryIndexedValue">PS</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=PS/@EntryIndexedValue">PS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=PSP/@EntryIndexedValue">PSP</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=PSP/@EntryIndexedValue">PSP</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RPCS/@EntryIndexedValue">RPCS</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RPCS/@EntryIndexedValue">RPCS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SMTP/@EntryIndexedValue">SMTP</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=TLS/@EntryIndexedValue">TLS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Method/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"&gt;&lt;ExtraRule Prefix="" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Method/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"&gt;&lt;ExtraRule Prefix="" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>

View file

@ -0,0 +1,48 @@
using System;
using System.Net;
using System.Net.Mail;
using LBPUnion.ProjectLighthouse.Types.Settings;
namespace LBPUnion.ProjectLighthouse.Helpers;
public static class SMTPHelper
{
private static readonly SmtpClient client;
private static readonly MailAddress fromAddress;
static SMTPHelper()
{
if (!ServerSettings.Instance.SMTPEnabled) return;
client = new SmtpClient(ServerSettings.Instance.SMTPHost, ServerSettings.Instance.SMTPPort)
{
EnableSsl = ServerSettings.Instance.SMTPSsl,
Credentials = new NetworkCredential(ServerSettings.Instance.SMTPFromAddress, ServerSettings.Instance.SMTPPassword),
};
fromAddress = new MailAddress(ServerSettings.Instance.SMTPFromAddress, ServerSettings.Instance.SMTPFromName);
}
public static bool SendEmail(string recipientAddress, string subject, string body)
{
if (!ServerSettings.Instance.SMTPEnabled) return false;
MailMessage message = new(fromAddress, new MailAddress(recipientAddress))
{
Subject = subject,
Body = body,
};
try
{
client.Send(message);
}
catch(Exception e)
{
Console.WriteLine(e);
return false;
}
return true;
}
}

View file

@ -12,7 +12,7 @@ namespace LBPUnion.ProjectLighthouse.Types.Settings;
[Serializable] [Serializable]
public class ServerSettings public class ServerSettings
{ {
public const int CurrentConfigVersion = 22; // MUST BE INCREMENTED FOR EVERY CONFIG CHANGE! public const int CurrentConfigVersion = 23; // MUST BE INCREMENTED FOR EVERY CONFIG CHANGE!
private static FileSystemWatcher fileWatcher; private static FileSystemWatcher fileWatcher;
static ServerSettings() static ServerSettings()
{ {
@ -162,6 +162,24 @@ public class ServerSettings
public string ServerListenUrl { get; set; } = "http://localhost:10060"; public string ServerListenUrl { get; set; } = "http://localhost:10060";
#region SMTP
public bool SMTPEnabled { get; set; }
public string SMTPHost { get; set; } = "";
public int SMTPPort { get; set; } = 587;
public string SMTPFromAddress { get; set; } = "lighthouse@example.com";
public string SMTPFromName { get; set; } = "Project Lighthouse";
public string SMTPPassword { get; set; } = "";
public bool SMTPSsl { get; set; } = true;
#endregion
#region Meta #region Meta
[NotNull] [NotNull]