129 lines
4.0 KiB
C#
129 lines
4.0 KiB
C#
using BS.Shared.AppSender;
|
|
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;
|
|
using BS.Shared.Core;
|
|
using BS.Shared.Extensions;
|
|
|
|
namespace BeWo.ServerUtils.Core
|
|
{
|
|
public static class MailUtils
|
|
{
|
|
private static readonly Module _GKV_Module = new Module("Gkv", "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-FDL8EJ28T7E"] = "app1.bewoplaner.de",
|
|
["WIN-E38DS2VNNPE"] = "app2.bewoplaner.de",
|
|
["WIN-40R404TI9J0"] = "app3.bewoplaner.de",
|
|
["OWNEROR-8BGG3AK"] = "app4.bewoplaner.de",
|
|
["OWNEROR-K9VRMQT"] = "app5.bewoplaner.de",
|
|
["WIN-1288MG5M1RE"] = "app6.bewoplaner.de",
|
|
["WIN-5H6NO5J7OAT"] = "app7.bewoplaner.de",
|
|
["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);
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
|
|
}
|
|
}
|