MailUtils umgezogen + ergänzt
This commit is contained in:
@@ -1,96 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BS.Shared.Core
|
||||
{
|
||||
internal struct Module
|
||||
{
|
||||
public string Short { get; set; }
|
||||
public string Name { get; set; }
|
||||
|
||||
public Module(string shortName, string name)
|
||||
{
|
||||
Short = shortName;
|
||||
Name = name;
|
||||
}
|
||||
};
|
||||
|
||||
internal struct SmtpConnectionInfo
|
||||
{
|
||||
public string SmtpServer { get; set; }
|
||||
public bool Enabled { get; set; }
|
||||
public string SmtpUser { get; set; }
|
||||
public string SmtpPassword { get; set; }
|
||||
public string MailSender { get; set; }
|
||||
public string MailReceiver { get; set; }
|
||||
}
|
||||
|
||||
public static class AppSettingReader
|
||||
{
|
||||
private static readonly string _AppSettingsSmtpServer = "SendMailModuleSmtpServer";
|
||||
private static readonly string _AppSettingsEnabledFormat = "SendMailModule{0}Enabled";
|
||||
private static readonly string _AppSettingsSmtpUserFormat = "SendMailModule{0}SmtpUser";
|
||||
private static readonly string _AppSettingsSmtpPasswordFormat = "SendMailModule{0}SmtpPassword";
|
||||
private static readonly string _AppSettingsSenderFormat = "SendMailModule{0}Sender";
|
||||
private static readonly string _AppSettingsReceiverFormat = "SendMailModule{0}Receiver";
|
||||
|
||||
#region SmtpConnectionInfo
|
||||
internal static SmtpConnectionInfo GetSmtpConnectionInfo(Module module)
|
||||
=> GetSmtpConnectionInfo(module.Short);
|
||||
internal static SmtpConnectionInfo GetSmtpConnectionInfo(string key)
|
||||
{
|
||||
return new SmtpConnectionInfo()
|
||||
{
|
||||
SmtpServer = GetSmtpServer(),
|
||||
Enabled = GetSendMailModuleEnabled(key),
|
||||
SmtpUser = GetSendMailModuleUser(key),
|
||||
SmtpPassword = GetSendMailModulePassword(key),
|
||||
MailSender = GetSendMailModuleSender(key),
|
||||
MailReceiver = GetSendMailModuleReceiver(key)
|
||||
};
|
||||
}
|
||||
private static string GetSmtpServer()
|
||||
{
|
||||
var setting_path = _AppSettingsSmtpServer;
|
||||
var setting = ConfigurationManager.AppSettings.Get(setting_path);
|
||||
return setting;
|
||||
}
|
||||
private static bool GetSendMailModuleEnabled(string key)
|
||||
{
|
||||
var setting_path = string.Format(_AppSettingsEnabledFormat, key);
|
||||
var setting = ConfigurationManager.AppSettings.Get(setting_path);
|
||||
if (bool.TryParse(setting, out var rtn))
|
||||
return rtn;
|
||||
return false;
|
||||
}
|
||||
private static string GetSendMailModuleUser(string key)
|
||||
{
|
||||
var setting_path = string.Format(_AppSettingsSmtpUserFormat, key);
|
||||
var setting = ConfigurationManager.AppSettings.Get(setting_path);
|
||||
return setting;
|
||||
}
|
||||
private static string GetSendMailModulePassword(string key)
|
||||
{
|
||||
var setting_path = string.Format(_AppSettingsSmtpPasswordFormat, key);
|
||||
var setting = ConfigurationManager.AppSettings.Get(setting_path);
|
||||
return setting;
|
||||
}
|
||||
private static string GetSendMailModuleSender(string key)
|
||||
{
|
||||
var setting_path = string.Format(_AppSettingsSenderFormat, key);
|
||||
var setting = ConfigurationManager.AppSettings.Get(setting_path);
|
||||
return setting;
|
||||
}
|
||||
private static string GetSendMailModuleReceiver(string key)
|
||||
{
|
||||
var setting_path = string.Format(_AppSettingsReceiverFormat, key);
|
||||
var setting = ConfigurationManager.AppSettings.Get(setting_path);
|
||||
return setting;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
using BS.Shared.AppSender;
|
||||
using DevExpress.Mvvm.Native;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BS.Shared.Core
|
||||
{
|
||||
public static class MailUtils
|
||||
{
|
||||
private static readonly Module _GKV_Module = new Module("Gkv", "GKV/EDI/EDAT");
|
||||
private static readonly Module _GKV_Module2 = new Module("GkvTest", "GKV/EDI/EDAT");
|
||||
private static readonly Module _AI_Module = new Module("Ai", "AI");
|
||||
|
||||
private static readonly Dictionary<string, string> _serverMap = new Dictionary<string, string>()
|
||||
{
|
||||
["WIN-A27H847STS6"] = "app8.bewoplaner.de",
|
||||
};
|
||||
|
||||
public static bool SendAiModuleErrorMail(string title, string body, string tenant)
|
||||
=> SendModuleErrorMail(_AI_Module, title, body, tenant);
|
||||
|
||||
public static bool SendGkvModuleErrorMail(string title, Exception e, string tenant)
|
||||
=> SendGkvModuleErrorMail(title, "Exception gefunden: " + e.ToString(), tenant);
|
||||
public static bool SendGkvModuleErrorMail(string title, string body, string tenant)
|
||||
{
|
||||
return SendModuleErrorMail(_GKV_Module, title, body, tenant);
|
||||
}
|
||||
|
||||
public static bool SendGkvModuleTestErrorMail(string title, string body, string tenant)
|
||||
{
|
||||
return SendModuleErrorMail(_GKV_Module2, title, body, tenant);
|
||||
}
|
||||
|
||||
private static bool SendModuleErrorMail(Module module, string title, string body, string tenant)
|
||||
{
|
||||
var conn = AppSettingReader.GetSmtpConnectionInfo(module);
|
||||
|
||||
if (!conn.Enabled)
|
||||
return false;
|
||||
|
||||
var subject = $"ownSoft Module Error {module.Name}: {title}";
|
||||
var toAddress = conn.MailReceiver;
|
||||
var senderName = conn.MailSender;
|
||||
var senderEMail = conn.MailSender;
|
||||
var smtpServer = conn.SmtpServer;
|
||||
var smtpUser = conn.SmtpUser;
|
||||
var smtpPassword = conn.SmtpPassword;
|
||||
var sendAsync = false;
|
||||
|
||||
string tenant_str = "Kunde: " + (tenant ?? "unknown");
|
||||
body += "\n\n" + tenant_str;
|
||||
|
||||
string server = AppRequestSender.GetServerFromTenant(tenant);
|
||||
string server_str = "Kundenserver: " + (server ?? "unkown");
|
||||
body += "\n" + server_str;
|
||||
|
||||
string hostname = Dns.GetHostName();
|
||||
string hostname_str = "MachineName: " + (hostname ?? "unknown");
|
||||
body += "\n\n" + hostname_str;
|
||||
|
||||
string server2 = _serverMap.GetValueOrDefault(hostname, "unknown");
|
||||
string server2_str = "ServerMap: " + (server2);
|
||||
body += "\n" + server2_str;
|
||||
|
||||
return SendEmail(subject, body, toAddress, senderName, senderEMail, smtpServer, smtpUser, smtpPassword, sendAsync);
|
||||
}
|
||||
|
||||
private static bool SendEmail(string subject, string body, string toAddress, string fromName, string fromAddress, string smtpServer, string smtpUser, string smtpPassword, bool sendAsync)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!IsValidEmail(toAddress) || !IsValidEmail(fromAddress))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
MailMessage message = new MailMessage(fromAddress, toAddress, subject, body);
|
||||
|
||||
if (toAddress != fromAddress)
|
||||
{
|
||||
message.ReplyToList.Add(new MailAddress(fromAddress, fromName));
|
||||
}
|
||||
|
||||
message.BodyEncoding = Encoding.UTF8;
|
||||
message.IsBodyHtml = false;
|
||||
|
||||
var mailClient = new SmtpClient(smtpServer, 587) { Credentials = new NetworkCredential(smtpUser, smtpPassword) };
|
||||
mailClient.EnableSsl = true;
|
||||
|
||||
if (sendAsync)
|
||||
{
|
||||
mailClient.SendAsync(message, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
mailClient.Send(message);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsValidEmail(string email)
|
||||
{
|
||||
//return Regex.IsMatch(email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
|
||||
|
||||
return Regex.IsMatch(email,
|
||||
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
|
||||
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
|
||||
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -145,7 +145,6 @@
|
||||
<Compile Include="Core\AiUtils.cs" />
|
||||
<Compile Include="Core\ApiResponseUtils.cs" />
|
||||
<Compile Include="Core\AppError.cs" />
|
||||
<Compile Include="Core\AppSettingReader.cs" />
|
||||
<Compile Include="Core\BeWoFault.cs" />
|
||||
<Compile Include="Core\ComplexWohnheimbuchungsRelationHelper.cs" />
|
||||
<Compile Include="Core\AbstractIDSpecificDefaultClass.cs" />
|
||||
@@ -153,7 +152,6 @@
|
||||
<Compile Include="Core\Facade\HttpClientFacade.cs" />
|
||||
<Compile Include="Core\Facade\JsonHttpClientFacade.cs" />
|
||||
<Compile Include="Core\JsonUtils.cs" />
|
||||
<Compile Include="Core\MailUtils.cs" />
|
||||
<Compile Include="Core\Rule\GkvRules.cs" />
|
||||
<Compile Include="Core\SecurityUtils.cs" />
|
||||
<Compile Include="Core\TokenUtils.cs" />
|
||||
|
||||
Reference in New Issue
Block a user