patbef-ServiceOutside/ServiceShared/Mail.cs

206 lines
7.3 KiB
C#
Raw Normal View History

2024-01-29 16:27:34 +01:00
using MailKit.Net.Smtp;
using MimeKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServiceShared
{
public class Mail
{
/// <summary>
/// Smtp server for sending mails
/// </summary>
private static string SMTPServer = null;
/// <summary>
/// Smtp server port for sending mails
/// </summary>
private static int SMTPPort = 25;
/// <summary>
/// Flag for using ssl for sending mails
/// </summary>
public static bool SMTPSSLUse = false;
/// <summary>
/// Username for authentification to smtp server
/// </summary>
public static string SMTPUsername { get; set; }
/// <summary>
/// Password for authentification to smtp server
/// </summary>
public static string SMTPPassword { get; set; }
/// <summary>
/// Smtp server sender email for sending mails
/// </summary>
private static string SMTPSenderEmail = null;
/// <summary>
/// Smtp server sender name(Display Name) for sending mails
/// </summary>
private static string SMTPSenderName = null;
/// <summary>
/// Smpt log recipients, separated by ; for sending mails
/// </summary>
private static string LogsRecipients = null;
/// <summary>
/// Smpt customer request recipients, separated by ; for sending mails
/// </summary>
private static string CustomerRequestsRecipients = null;
/// <summary>
/// Sets the smtp data
/// </summary>
/// <param name="server">Host/IP for smtp server</param>
/// <param name="port">Port for Smtp server</param>
/// <param name="ssl">Flag if Smtp server is using ssl</param>
/// <param name="senderEmail">Sender Email for Smtp logs</param>
/// <param name="senderName">Sender Name for Smtp logs</param>
/// <param name="logsRecipients">Recipients of Smtp logs, separated by ;</param>
/// <param name="customerRequestsRecipients">Recipients of customer requests, separated by ;</param>
public static void SetSMTPData(string server, int port, bool ssl, string senderEmail, string senderName, string logsRecipients, string customerRequestsRecipients, string username, string password)
{
SMTPServer = server;
SMTPPort = port;
SMTPSSLUse = ssl;
SMTPSenderEmail = senderEmail;
SMTPSenderName = senderName;
LogsRecipients = logsRecipients;
CustomerRequestsRecipients = customerRequestsRecipients;
SMTPUsername = username;
SMTPPassword = password;
}
/// <summary>
/// Send the customer request mail with subject and body to the recipients
/// </summary>
/// <param name="_subject">subject of mail</param>
/// <param name="_body">body of mail</param>
public static bool SendCustomerRequest(string _subject, string _body)
{
bool result = false;
try
{
if(!String.IsNullOrEmpty(SMTPServer) && SMTPPort > 0 && !string.IsNullOrEmpty(CustomerRequestsRecipients))
{
string[] recipients = null;
if (CustomerRequestsRecipients.Contains(";"))
{
recipients = CustomerRequestsRecipients.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
}
else
{
recipients = new string[] { CustomerRequestsRecipients };
}
result = Send("Customer Request - " + _subject, _body, recipients);
}
}
catch (Exception ex)
{
Log.Error(ex, "ServiceShared.Mail", "SendCustomerRequest(string, string)");
}
return result;
}
/// <summary>
/// Send log mail with subject and body to the recipients
/// </summary>
/// <param name="_subject">subject of mail</param>
/// <param name="_body">body of mail</param>
public static void SendLogs(string _subject, string _body)
{
try
{
if (!String.IsNullOrEmpty(SMTPServer) && SMTPPort > 0 && !string.IsNullOrEmpty(LogsRecipients))
{
string[] recipients = null;
if (LogsRecipients.Contains(";"))
{
recipients = LogsRecipients.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
}
else
{
recipients = new string[] { LogsRecipients };
}
Send("LOG - " + _subject, _body, recipients);
}
}
catch (Exception ex)
{
Log.Error(ex, "ServiceShared.Mail", "SendCustomerRequest(string, string)");
}
}
/// <summary>
/// Sends mail to recipients with subject and body
/// </summary>
/// <param name="subject">subject of mail</param>
/// <param name="body">text of maily</param>
/// <param name="recipients">recipients</param>
/// <returns></returns>
private static bool Send(string subject, string body, string[] recipients)
{
bool result = false;
try
{
using (SmtpClient client = new SmtpClient())
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
Log.Debug("Mail connecting to server: " + SMTPServer + ":" + SMTPPort + (SMTPSSLUse ? "(SSL)" : ""));
client.Connect(SMTPServer, SMTPPort, SMTPSSLUse);
if (!string.IsNullOrEmpty(SMTPUsername) && !string.IsNullOrEmpty(SMTPPassword))
{
Log.Debug("Mail authentificate to server: " + SMTPServer + ":" + SMTPPort + (SMTPSSLUse ? "(SSL)" : ""));
client.Authenticate(SMTPUsername, SMTPPassword);
}
MimeMessage message = new MimeMessage();
message.From.Add(new MailboxAddress((string.IsNullOrEmpty(SMTPSenderName) ? SMTPSenderName : SMTPSenderEmail), SMTPSenderEmail));
foreach (string recipient in recipients)
{
message.To.Add(new MailboxAddress(null, recipient));
}
message.Subject = subject;
BodyBuilder builder = new BodyBuilder();
builder.TextBody = body;
message.Body = builder.ToMessageBody();
Log.Debug("Mail sending: " + subject + ", " + body);
client.Send(message);
client.Disconnect(true);
Log.Debug("Mail sent: " + subject + ", " + body);
result = true;
}
}
catch (Exception ex)
{
Log.Error(ex, "ServiceShared.Mail", "Send(string, string, string[])");
}
return result;
}
}
}