Files
BeWoPlaner/Shared/Core/MailUtils.cs
2026-03-04 11:14:03 +01:00

118 lines
3.6 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;
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");
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 = "Hostname: " + (hostname ?? "unknown");
body += "\n" + hostname_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));
}
}
}