using betrace_carfi_erp_driver.Common;
using betrace_carfi_erp_driver.Components;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.Reflection;


namespace betrace_carfi_erp_driver.Common
{

    class Mail
    {

        public SmtpClient smtp;
        private MailMessage message;
        public void TrataEmail(string EnderecoDestino,  string CorpoMail, string DescritivoRemetente, string Assunto, string CC = "", string BCC = "", string StrErro = "", string user = "", string nome_user = "")
        {
            smtp = new SmtpClient();
            message = new MailMessage();
            try
            {
                string Server = SystemInfo.BeTrace_SQL_Server;
                string Bdados = SystemInfo.BeTrace_SQL_Database;
                string BdadosERP = SystemInfo.ERP_SQL_Database;
                string Versao = Assembly.GetExecutingAssembly().GetName().Version.ToString();
                string DataAtual = DateTime.Now.ToString();
                string CabCorpoHtml = "";
                message.IsBodyHtml = true;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                string username = "noreply@carfi.pt";
                string password = "no123";
                smtp.Host = "mail.carfi.pt";
                smtp.EnableSsl = false;
                smtp.Port = 25;
                smtp.Credentials = new NetworkCredential(username, password);
                message.To.Add(EnderecoDestino);
                if (!(CC == ""))
                {
                    message.CC.Add(CC);
                }
                if (!(BCC == ""))
                {
                    message.Bcc.Add(BCC);
                }
                message.From = new MailAddress("noreply@carfi.pt", DescritivoRemetente);
                message.Subject = Assunto;

                CabCorpoHtml = @"<style type='text/css'>
                 .tg  { border-collapse:collapse; border-spacing:0; border-color:#999;border:none;}
                    td{ font-family:Arial, sans-serif; font-size:14px; padding: 10px 5px; border-style:solid; border-width:0px; overflow: hidden; word-break:normal; border-color:#999;color:#444;background-color:#F7FDFA;}
                    th{ font-family:Arial, sans-serif; font-size:14px; font-weight:normal; padding: 10px 5px; border-style:solid; border-width:0px; overflow: hidden; word-break:normal; border-color:#999;color:#fff;background-color:#26ADE4;}
                  .tg .tg-yw4l{ vertical-align:top;}
              </style>
                 <table class='tg'>
                   <tr>
                    <th class='tg-b7b8' colspan='2'>* B E  T R A C E V 4 *</th>
                   </tr>
                   <tr>
                     <td class='tg-b7b8'>Data</td>
                     <td class='tg-b7b8'>" + DataAtual + @"</td>
                   </tr>
                   <tr>
                     <td class='tg'>Servidor SQL</td>
                     <td class='tg'>" + Server + @"</td>
                   </tr>
                   <tr>
                     <td class='tg-yw4l'>Base de Dados</td>
                     <td class='tg-yw4l'>" + Bdados + @"</td>
                   </tr>
                   <tr>
                     <td class='tg-b7b8'>Base de Dados ERP</td>
                     <td class='tg-b7b8'>" + BdadosERP + @"</td>
                   </tr>
                   <tr>
                     <td class='tg-yw4l'>Versão Be-Trace</td>
                     <td class='tg-yw4l'>" + Versao + @"</td>
                   </tr>
                 </table>";

                  message.Body = CabCorpoHtml;
               message.Body += "</br></br>" + CorpoMail;


                //message.Body += CorpoMail;
        smtp.Send(message);
                
            }
            catch {
               
                StrErro = "Erro: ";
            }
            finally
            {
                smtp = null;
                message = null;
            }
         }


        }


        
    }

