using System;
using System.Net.Mail;
using System.Text;
using Baza_Reklam.Classes.Helpers;

namespace Baza_Reklam.Classes
{
    public class EmailSender
    {
        public static void SendNotification(Exception loggingException, Exception exception, string userName, DateTime dateTime)
        {
            try
            {
                MailMessage message = new MailMessage();
                message.From = new MailAddress(ConfigurationHelper.GetSettingByKey("Application.Email.From") ?? "baza_reklam@ct.com.pl");
                message.To.Add(new MailAddress(ConfigurationHelper.GetSettingByKey("Application.Email.To") ?? "marek.stachura@ct.com.pl"));
                message.Subject = "Error Logging Exception - " + dateTime;

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Data i czas: " + dateTime);
                sb.AppendLine("Użytkownik: " + userName);
                sb.AppendLine("Exception.Message: " + exception.Message);
                sb.AppendLine(exception.ToString());
                sb.AppendLine("***");
                sb.AppendLine("Logging.Exception.Message: " + loggingException.Message);
                sb.AppendLine(loggingException.ToString());

                message.Body = sb.ToString();
                SmtpClient client = new SmtpClient(ConfigurationHelper.GetSettingByKey("Application.Email.Smtp") ?? "poczta.ct.com.pl");
                client.SendAsync(message, null);
            }
            catch
            {
                //TODO: nothing what we can do now...
            }

        }
    }
}
