Utils SendMail bisschen umgeschrieben, UnitTest für Mail send, GkvServerGetStatusRequest Error Mail Einbindung

This commit is contained in:
2024-11-18 11:48:14 +01:00
parent cd1f2ebdd8
commit 8b75149950
5 changed files with 276 additions and 228 deletions

View File

@@ -38,7 +38,7 @@ namespace DakotaUnitTest
{
var count = Reader.GetAllFileEntryCount();
Assert.IsTrue(count == 1105);
Assert.IsTrue(count == 1106);
}
[TestMethod]

View File

@@ -10,9 +10,12 @@ namespace DakotaUnitTest
[TestMethod]
public void SendTestMail()
{
var subject = "";
var title = "TEST - UNITTEST";
var body = "TEST - BODY";
//var success = Utils.SendMail(subject, body, sende)
var success = Utils.SendDakotaErrorMail(title, body);
Assert.IsTrue(success);
}
}
}

View File

@@ -216,7 +216,7 @@
<add key="SendMailOnError" value="false" />
<add key="SmptServer" value="mail.ownsoft.de" />
<add key="SmtpUser" value="bewoplaner.support@ownsoft.de" />
<add key="SmtpPassword" value="G57ojFhgZ" />
<add key="SmtpPassword" value="JJVjJntw2yaRDAxInotF" />
<add key="Recipient" value="error@bewoplaner.de" />
<add key="SupportEMail" value="support@bewoplaner.de" />
<add key="LicenseInfoUrl" value="https://support.bewoplaner.de/api/getlicensecount.php?k=[TENANT]" />

View File

@@ -21,132 +21,177 @@ using BS.Shared.Extensions;
namespace BeWo.Service.Core
{
public class Utils
{
private static readonly Regex indexRegex = new Regex("(?:Index=\"){1}([0-9]+)\"{1}");
private static readonly Regex idRegex = new Regex("(?:Id=\")(([a-z0-9]{8})-{1}([a-z0-9]{4})-{1}([a-z0-9]{4})-{1}([a-z0-9]{4})-{1}([a-z0-9]{12}))(?=\"{1})");
public class Utils
{
private static readonly Regex indexRegex = new Regex("(?:Index=\"){1}([0-9]+)\"{1}");
private static readonly Regex idRegex = new Regex("(?:Id=\")(([a-z0-9]{8})-{1}([a-z0-9]{4})-{1}([a-z0-9]{4})-{1}([a-z0-9]{4})-{1}([a-z0-9]{12}))(?=\"{1})");
public static FaultException<BeWoFault> CreateBeWoFaultException(Exception ex)
{
try
{
//SendErrorMail("MYSQL EXCEPTION - AUTOMATED MAIL NOTIFICATION", ex.ToString());
}
catch
{
}
private static readonly string _GKV_Module_Name = "GKV/EDI";
return new FaultException<BeWoFault>(new BeWoFault(ex), ex.ToString());
}
public static string GetClientIP()
{
if (OperationContext.Current != null)
{
MessageProperties properties = OperationContext.Current.IncomingMessageProperties;
var endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
return endpoint.Address;
}
return null;
}
public static string GetMD5(string val)
{
var lMD5 = new MD5CryptoServiceProvider();
string lHash = BitConverter.ToString(lMD5.ComputeHash(Encoding.UTF8.GetBytes(val)));
return lHash;
}
public static string GetSHA256(string val)
{
var csp = new SHA256CryptoServiceProvider();
string lHash = BitConverter.ToString(csp.ComputeHash(Encoding.UTF8.GetBytes(val)));
return lHash;
}
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));
}
public static bool SendErrorMail(string subject, string body)
{
string sendMailSetting = ConfigurationManager.AppSettings.Get("SendMailOnError");
bool sendMail = false;
if (bool.TryParse(sendMailSetting, out sendMail))
{
if (sendMail)
return SendMail(subject, body, ConfigurationManager.AppSettings.Get("Recipient"));
}
return false;
}
public static bool SendMail(string subject, string body, string toAddress)
{
return SendMail(subject, body, toAddress, null, null);
}
public static bool SendMail(string subject , string body, string toAddress, string senderName, string senderEMail)
private static readonly Dictionary<string, string> _Module2Email = new Dictionary<string, string>()
{
var server = ConfigurationManager.AppSettings.Get("SmptServer");
var user = ConfigurationManager.AppSettings.Get("SmtpUser");
//var pass = ConfigurationManager.AppSettings.Get("SmtpPassword");
var pass = "JJVjJntw2yaRDAxInotF";
String to = toAddress;
if (String.IsNullOrEmpty(to))
{ _GKV_Module_Name, "dakota.exchange@ownsoft.de"}
};
public static FaultException<BeWoFault> CreateBeWoFaultException(Exception ex)
{
try
{
//SendErrorMail("MYSQL EXCEPTION - AUTOMATED MAIL NOTIFICATION", ex.ToString());
}
catch
{
}
return new FaultException<BeWoFault>(new BeWoFault(ex), ex.ToString());
}
public static string GetClientIP()
{
if (OperationContext.Current != null)
{
MessageProperties properties = OperationContext.Current.IncomingMessageProperties;
var endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
return endpoint.Address;
}
return null;
}
public static string GetMD5(string val)
{
var lMD5 = new MD5CryptoServiceProvider();
string lHash = BitConverter.ToString(lMD5.ComputeHash(Encoding.UTF8.GetBytes(val)));
return lHash;
}
public static string GetSHA256(string val)
{
var csp = new SHA256CryptoServiceProvider();
string lHash = BitConverter.ToString(csp.ComputeHash(Encoding.UTF8.GetBytes(val)));
return lHash;
}
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));
}
public static bool SendDakotaErrorMail(string title, Exception e)
{
return SendDakotaErrorMail(title, e.ToString());
}
public static bool SendDakotaErrorMail(string title, string body)
{
return SendModuleErrorMail(_GKV_Module_Name, title, body);
}
private static bool SendModuleErrorMail(string module, string title, string body)
{
var subject = $"ownSoft Module Error {module}: {title}";
var toAddress = _Module2Email[module];
var senderName = "noreply";
var senderEMail = "noreply@bewoplaner.de";
var smtpServer = "mail.ownsoft.de";
var smtpUser = "bewoplaner.support@ownsoft.de";
return SendMail(subject, body, toAddress, senderName, senderEMail, true, smtpServer, smtpUser);
}
public static bool SendErrorMail(string subject, string body)
{
string sendMailSetting = ConfigurationManager.AppSettings.Get("SendMailOnError");
bool sendMail = false;
if (bool.TryParse(sendMailSetting, out sendMail))
{
if (sendMail)
return SendMail(subject, body, ConfigurationManager.AppSettings.Get("Recipient"));
}
return false;
}
//public static bool SendMail(string subject, string body, string toAddress)
// {
// return SendMail(subject, body, toAddress, null, null);
// }
public static bool SendMail(string subject, string body, string toAddress,
string senderName = null, string senderEMail = null, bool skipTenant = false,
string smtpServer = null, string smtpUser = null, string smtpPassword = null)
{
if (string.IsNullOrEmpty(smtpServer))
smtpServer = ConfigurationManager.AppSettings.Get("SmptServer");
if (string.IsNullOrEmpty(smtpUser))
smtpUser = ConfigurationManager.AppSettings.Get("SmtpUser");
if (string.IsNullOrEmpty(smtpPassword))
smtpPassword = ConfigurationManager.AppSettings.Get("SmtpPassword");
if (string.IsNullOrEmpty(smtpPassword))
smtpPassword = "JJVjJntw2yaRDAxInotF";
string to = toAddress;
if (string.IsNullOrEmpty(to))
to = ConfigurationManager.AppSettings.Get("SupportEMail");
if (String.IsNullOrEmpty(to))
if (string.IsNullOrEmpty(to))
to = "support@bewoplaner.de";
if (String.IsNullOrEmpty(senderEMail))
if (string.IsNullOrEmpty(senderEMail))
senderEMail = "bewoapp@bewoplaner.de";
bool sendAsync = true;
if (to.Equals("support@bewoplaner.de"))
{
sendAsync = false;
}
var body2 = "Kunde: " + MultitenancyOperationContextExt.Current.Tenant;
bool sendAsync = true;
if (to.Equals("support@bewoplaner.de"))
{
sendAsync = false;
}
string body2;
if (skipTenant)
{
body2 = "Kunde: " + (MultitenancyOperationContextExt.Current?.Tenant ?? "unknown");
sendAsync = false;
}
else
{
body2 = "Kunde: " + MultitenancyOperationContextExt.Current.Tenant;
//body2 += "\r\nIP: " + GetClientIP();
body += "\n\n" + body2;
//}
}
body += "\n\n" + body2;
senderEMail = senderEMail.Trim();
return SendMail2(subject, body, to, senderName, senderEMail, smtpServer, smtpUser, smtpPassword, sendAsync);
}
private static bool SendMail2(string subject, string body, string toAddress, string senderName, string senderEMail, string smtpServer, string smtpUser, string smtpPassword, bool sendAsync)
{
try
{
if (!IsValidEmail(toAddress) || !IsValidEmail(senderEMail))
{
return false;
}
String from = senderEMail;
if (toAddress.Equals("support@bewoplaner.de"))
{
var supportSenderMail = ConfigurationManager.AppSettings.Get("SupportSenderEMail");
if (String.IsNullOrEmpty(supportSenderMail))
supportSenderMail = "noreply@bewoplaner.de";
from = supportSenderMail;
}
//if (to.Equals())
senderEMail = senderEMail.Trim();
return SendMail(subject, body, to, senderName, senderEMail, server, user, pass, sendAsync);
}
public static bool SendMail(string subject, string body, string toAddress, string senderName, string senderEMail, string smtpServer, string smtpUser, string smtpPassword, bool sendAsync)
{
try
{
if (!IsValidEmail(toAddress) || !IsValidEmail(senderEMail))
{
return false;
}
String from = senderEMail;
if (toAddress.Equals("support@bewoplaner.de"))
{
var supportSenderMail = ConfigurationManager.AppSettings.Get("SupportSenderEMail");
if (String.IsNullOrEmpty(supportSenderMail))
supportSenderMail = "noreply@bewoplaner.de";
from = supportSenderMail;
}
MailMessage message = null;
MailMessage message = null;
if (String.IsNullOrEmpty(senderName))
message = new MailMessage(from, toAddress, subject, body);
else
@@ -158,41 +203,41 @@ namespace BeWo.Service.Core
message.Subject = subject;
message.Body = body;
}
if (senderEMail != from)
{
message.ReplyToList.Add(new MailAddress(senderEMail, senderName));
}
if (senderEMail != from)
{
message.ReplyToList.Add(new MailAddress(senderEMail, senderName));
}
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = false;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = false;
var mailClient = new SmtpClient(smtpServer, 587) { Credentials = new NetworkCredential(smtpUser, smtpPassword) };
mailClient.EnableSsl = true;
var mailClient = new SmtpClient(smtpServer, 587) { Credentials = new NetworkCredential(smtpUser, smtpPassword) };
mailClient.EnableSsl = true;
if (sendAsync)
{
mailClient.SendAsync(message, null);
}
else
{
mailClient.Send(message);
}
if (sendAsync)
{
mailClient.SendAsync(message, null);
}
else
{
mailClient.Send(message);
}
return true;
}
catch
{
return false;
}
}
return true;
}
catch (Exception e)
{
return false;
}
}
public static string[][] T(List<string> pSearchParameter, int pSearchFieldCount)
{
//List<string> lDifferentParameter;
public static string[][] T(List<string> pSearchParameter, int pSearchFieldCount)
{
//List<string> lDifferentParameter;
return null;
}
return null;
}
public static string GetRecurringAppointmentsId(SchedulerAppointment appointment)
{
@@ -232,102 +277,102 @@ namespace BeWo.Service.Core
}
public static bool UserHasRight(ApplicationUser user, UserRightType right)
{
return GetGrantedRights(user).Contains(right);
public static bool UserHasRight(ApplicationUser user, UserRightType right)
{
return GetGrantedRights(user).Contains(right);
}
}
public static List<UserRightType> GetGrantedRights(ApplicationUser user)
{
var rights = new List<UserRightType>();
foreach (var ug in user.UserGroups)
{
foreach (var rr in ug.Rights)
{
rights.Add(rr.RightType);
}
}
public static List<UserRightType> GetGrantedRights(ApplicationUser user)
{
var rights = new List<UserRightType>();
foreach (var ug in user.UserGroups)
{
foreach (var rr in ug.Rights)
{
rights.Add(rr.RightType);
}
}
return rights;
}
return rights;
}
public static string GetRecurrenceIdFromRecurrenceInfo(string pRecurrenceInfo)
{
var regex = new Regex("(Id=\\\"[a-z0-9-]+\\\")");
public static string GetRecurrenceIdFromRecurrenceInfo(string pRecurrenceInfo)
{
var regex = new Regex("(Id=\\\"[a-z0-9-]+\\\")");
var match = regex.Match(pRecurrenceInfo);
if(match.Success)
{
var value = match.Value;
var actualId = value.Split("\"");
if(actualId.Count > 1)
{
return actualId[1];
}
}
var match = regex.Match(pRecurrenceInfo);
if (match.Success)
{
var value = match.Value;
var actualId = value.Split("\"");
if (actualId.Count > 1)
{
return actualId[1];
}
}
return null;
}
return null;
}
public static string GetRecurrenceIndexFromRecurrenceInfo(string pRecurrenceInfo)
{
if(!pRecurrenceInfo.Contains("Index"))
{
return null;
}
public static string GetRecurrenceIndexFromRecurrenceInfo(string pRecurrenceInfo)
{
if (!pRecurrenceInfo.Contains("Index"))
{
return null;
}
var regex = new Regex("(Index=\\\"\\d+\\\")");
var regex = new Regex("(Index=\\\"\\d+\\\")");
var match = regex.Match(pRecurrenceInfo);
if(match.Success)
{
var value = match.Value;
var actualIndex = value.Split("\"");
var match = regex.Match(pRecurrenceInfo);
if (match.Success)
{
var value = match.Value;
var actualIndex = value.Split("\"");
if(actualIndex.Count > 1)
{
return actualIndex[1];
}
}
if (actualIndex.Count > 1)
{
return actualIndex[1];
}
}
return null;
}
return null;
}
public static List<string> ExtractRecurrenceIdFromRecurrenceString(List<SchedulerAppointment> appointments)
{
var result = new List<string>();
public static List<string> ExtractRecurrenceIdFromRecurrenceString(List<SchedulerAppointment> appointments)
{
var result = new List<string>();
foreach(var recurrenceInfo in appointments.Where(w => w.RecurrenceInfo != null).Select(s => s.RecurrenceInfo))
{
var id = GetRecurrenceIdFromRecurrenceInfo(recurrenceInfo);
foreach (var recurrenceInfo in appointments.Where(w => w.RecurrenceInfo != null).Select(s => s.RecurrenceInfo))
{
var id = GetRecurrenceIdFromRecurrenceInfo(recurrenceInfo);
if(id != null)
{
result.AddIfNotIn(id);
}
}
return result;
}
if (id != null)
{
result.AddIfNotIn(id);
}
}
public static string ExtractIdFromRecurrenceInfo(string pRecurrenceInfo)
{
return pRecurrenceInfo != null ? idRegex.Match(pRecurrenceInfo).Groups[1].Value : null;
}
return result;
}
public static int ExtractIndexFromRecurrenceInfo(string pRecurrenceInfo)
{
if (pRecurrenceInfo != null)
{
var indexString = indexRegex.Match(pRecurrenceInfo).Groups[1].Value;
public static string ExtractIdFromRecurrenceInfo(string pRecurrenceInfo)
{
return pRecurrenceInfo != null ? idRegex.Match(pRecurrenceInfo).Groups[1].Value : null;
}
var index = !string.IsNullOrEmpty(indexString) ? int.Parse(indexString) : 0;
public static int ExtractIndexFromRecurrenceInfo(string pRecurrenceInfo)
{
if (pRecurrenceInfo != null)
{
var indexString = indexRegex.Match(pRecurrenceInfo).Groups[1].Value;
return index;
}
var index = !string.IsNullOrEmpty(indexString) ? int.Parse(indexString) : 0;
return -1;
}
}
return index;
}
return -1;
}
}
}

View File

@@ -1195,7 +1195,7 @@ namespace BeWo.Service.ServiceImplementations
}
catch (Exception e)
{
throw e;
Utils.SendDakotaErrorMail("GkvServerGetStatusRequest Error", e);
return true;
}