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
{
///
/// Smtp server for sending mails
///
private static string SMTPServer = null;
///
/// Smtp server port for sending mails
///
private static int SMTPPort = 25;
///
/// Flag for using ssl for sending mails
///
public static bool SMTPSSLUse = false;
///
/// Username for authentification to smtp server
///
public static string SMTPUsername { get; set; }
///
/// Password for authentification to smtp server
///
public static string SMTPPassword { get; set; }
///
/// Smtp server sender email for sending mails
///
private static string SMTPSenderEmail = null;
///
/// Smtp server sender name(Display Name) for sending mails
///
private static string SMTPSenderName = null;
///
/// Smpt log recipients, separated by ; for sending mails
///
private static string LogsRecipients = null;
///
/// Smpt customer request recipients, separated by ; for sending mails
///
private static string CustomerRequestsRecipients = null;
///
/// Sets the smtp data
///
/// Host/IP for smtp server
/// Port for Smtp server
/// Flag if Smtp server is using ssl
/// Sender Email for Smtp logs
/// Sender Name for Smtp logs
/// Recipients of Smtp logs, separated by ;
/// Recipients of customer requests, separated by ;
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;
}
///
/// Send the customer request mail with subject and body to the recipients
///
/// subject of mail
/// body of mail
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;
}
///
/// Send log mail with subject and body to the recipients
///
/// subject of mail
/// body of mail
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)");
}
}
///
/// Sends mail to recipients with subject and body
///
/// subject of mail
/// text of maily
/// recipients
///
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;
}
}
}