using EmailManager.SendEmailWS; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Text; using System.Configuration; using System.Diagnostics; using System.IO; namespace EmailManager { public class Email { private static Log logger = new Log(ConfigurationManager.AppSettings["pathLog"]); static int Main(string[] args) { return Send(args); } private static int Send(string[] args) { string ambiente = ConfigurationManager.AppSettings["ambiente"]; string cawTo_Bin = ConfigurationManager.AppSettings["cawTo_Bin"]; string tngServer = args[0]; string tngJobset = args[1]; string tngJob = args[2]; string funzione = "GESTORE EMAIL"; string fac_I_100 = ConfigurationManager.AppSettings["fac_I_100"]; string fac_T_100 = ConfigurationManager.AppSettings["fac_T_100"]; string fac_E_100 = ConfigurationManager.AppSettings["fac_E_100"]; string argomentiCawToInizio = "-n " + tngServer + " " + fac_I_100 + " " + tngJobset + " - " + tngJob + " - Funzione " + funzione + " INIZIATA"; string argomentiCawToTerminato = "-n " + tngServer + " " + fac_T_100 + " " + tngJobset + " - " + tngJob + " - Funzione " + funzione + " terminata con SUCCESSO"; string argomentiCawToErrore = "-n " + tngServer + " " + fac_E_100 + " " + tngJobset + " - " + tngJob + " - Funzione "; //+funzione + " terminata con ERRORE"; try { //START StartTng(ambiente, cawTo_Bin, argomentiCawToInizio); string queryListaEmail = ConfigurationManager.AppSettings["SP_LISTA_EMAIL_DA_INVIARE"]; string queryUpdateEmail = ConfigurationManager.AppSettings["SP_UPDATE_EMAIL_INVIATE"]; IDataReader emails = DataAccess.ExecuteDataReaderStoredProcedure(DBProvider.SqlServerStampeC6, queryListaEmail, null); SendEmail sendEmailWS = new SendEmail(); while (emails.Read()) { int id = (int)emails["Email_Id"]; string da = (string)emails["E_From"]; string a = null; try { a = emails["E_To"].ToString(); } catch { string missingEntryProcedure = ConfigurationManager.AppSettings["SP_BP_DISCARDED_ADDR_MISSING_ADD_ENTRY"]; List parameters = new List(); parameters.Add(new Parametro() { DbType = DbType.Int32, ParameterName = "EmailId", Value = id }); parameters.Add(new Parametro() { DbType = DbType.String, ParameterName = "Description", Value = "E_To field missing in email informations." }); DataAccess.ExecuteNonQueryStoredProcedure(DBProvider.SqlServerStampeC6, missingEntryProcedure, parameters); Console.WriteLine(String.Format("E_To field missing in email informations. Email id: '{0}'", id)); logger.Write(String.Format("E_To field missing in email informations. Email id: '{0}'", id)); continue; } string cc = (string)emails["E_Cc"]; string bcc = (string)emails["E_Ccn"]; string oggetto = (string)emails["E_Subject"]; string corpo = (string)emails["Testo"]; List allegati = null; //if (emails["Cc"] != DBNull.Value) // cc = (string)emails["Cc"]; //if (emails["Bcc"] != DBNull.Value) // bcc = (string)emails["Bcc"]; //if (emails["Oggetto"] != DBNull.Value) // oggetto = (string)emails["Oggetto"]; //if (emails["Corpo"] != DBNull.Value) // corpo = (string)emails["Corpo"]; //List parametro = new List(); //Parametro pId = new Parametro(); //pId.DbType = DbType.Int32; //pId.ParameterName = "Email_Id"; //pId.Value = id; //parametro.Add(pId); //SqlDataReader attachments = (SqlDataReader)DataAccess.ExecuteDataReaderStoredProcedure(DBProvider.SqlServer, "sp_getEmailAllegati", parametro); //if (attachments.HasRows) { // allegati = new List(); // while (attachments.Read()) // allegati.Add((byte[])attachments["Allegato"]); //} bool res = sendEmailWS.SendMail(da, a, cc, bcc, oggetto, corpo, allegati == null ? null : allegati.ToArray()); int righe = 0; if (res) { List parametri = new List(); Parametro pId = new Parametro(); pId.DbType = DbType.Int32; pId.ParameterName = "Email_Id"; pId.Value = id; parametri.Add(pId); Parametro pDataInvio = new Parametro(); pDataInvio.DbType = DbType.DateTime; pDataInvio.ParameterName = "DataInvio"; pDataInvio.Value = DateTime.Now; parametri.Add(pDataInvio); righe = DataAccess.ExecuteNonQueryStoredProcedure(DBProvider.SqlServerStampeC6, queryUpdateEmail, parametri); } if (!res) { throw new Exception("Non è stata inviata l'email id:" + id.ToString()); } if (righe <= 0) { throw new Exception("Non è stato fatto l'update dell'email id:" + id.ToString()); } } //V,M Controlli automatici C6 (Quando si migra al framework 3.5 richiamare direttamente l'exe come libreria //come facciamo con la gestioneFlussoGiornaliero nell'invio tramite ftp [libreria smartFTPThread]) string path = ConfigurationManager.AppSettings["AutoC6ChecksPath"]; if (File.Exists(path)) { System.Diagnostics.Process processo_controlli = new Process(); processo_controlli.StartInfo.FileName = path; processo_controlli.Start(); while (!processo_controlli.HasExited) ; } else { ScriviErroreNelDB(666, "Mancata chiamata autoc6checks: " + path, 1030, "GESTOREPDF", "GESTOREPDF"); } // //FINISH StartTng(ambiente, cawTo_Bin, argomentiCawToTerminato); return 0; } catch (DataBaseException ex) { try { funzione = "ScriviErroreNelDB"; ScriviErroreNelDB(-292929, "Errore nel Gestore EMAIL: " + getDataBaseException(ex), -292929, "Gestore EMAIL", "Gestore EMAIL"); funzione = "GESTORE EMAIL"; } catch { } argomentiCawToErrore += funzione + " terminata con ERRORE"; StartTng(ambiente, cawTo_Bin, argomentiCawToErrore); return 1; } catch (Exception ex) { try { funzione = "ScriviErroreNelDB"; ScriviErroreNelDB(-292929, "Errore nel Gestore EMAIL: " + getException(ex), -292929, "Gestore EMAIL", "Gestore EMAIL"); funzione = "GESTORE EMAIL"; } catch { } argomentiCawToErrore += funzione + " terminata con ERRORE"; StartTng(ambiente, cawTo_Bin, argomentiCawToErrore); return 1; } } private static string getDataBaseException(DataBaseException ecc) { const string aCapo = "#/n#"; string ritorno = ""; ritorno += aCapo; ritorno += " Informazioni Comando SQL:" + aCapo; ritorno += " sql file name: " + ecc.SqlFileName + aCapo; ritorno += " connection string: " + ecc.ConnectionStringWithoutCredentials + aCapo; //ritorno += " command text: " + ecc.CommandText + aCapo; //controllo se ci sono i parametri if (ecc.Parameters != null) { foreach (Parametro parametro in ecc.Parameters) { ritorno += aCapo; ritorno += " parametro: " + parametro.ParameterName + aCapo; ritorno += " dimensione: " + parametro.Size.ToString() + aCapo; ritorno += " direzione: " + parametro.Direction.ToString() + aCapo; ritorno += " tipo: " + parametro.DbType.ToString() + aCapo; if (parametro.Value == null) ritorno += " valore: null" + aCapo; else { string valoreParametro = ""; if (parametro.Size > 500) { valoreParametro = " valore: null(sono visualizzati solo i primi 500 bytes): " + parametro.Value.ToString().Substring(0, 500); } else { valoreParametro = " valore: " + parametro.Value.ToString(); } ritorno += valoreParametro + aCapo; } } } ritorno += aCapo; ritorno += " tipo: DataBaseException" + aCapo; ritorno += " descrizione: " + ecc.Eccezione.Message + aCapo; ritorno += " sorgente: " + ecc.Eccezione.Source + aCapo; ritorno += " traccia: " + FormattaErrore(ecc.StackTrace) + aCapo; return ritorno; } private static string getException(Exception ecc) { const string aCapo = "#/n#"; string ritorno = ""; ritorno += aCapo; ritorno += " tipo: " + ecc.GetType().ToString() + aCapo; ritorno += " descrizione: " + ecc.Message + aCapo; ritorno += " sorgente: " + ecc.Source + aCapo; if (ecc.InnerException != null) { ritorno += " descrizione Inner: " + ecc.InnerException.Message + aCapo; ritorno += " sorgente Inner: " + ecc.InnerException.Source + aCapo; } ritorno += " traccia: " + FormattaErrore(ecc.StackTrace) + aCapo; return ritorno; } private static string FormattaErrore(string errore) { const string aCapo = "#/n#"; const string spazio = " "; string separatore1 = " at "; string separatore2 = " in "; string separatore3 = ":line "; string erroreInterno = errore.Replace(separatore1, "@"); string ritorno = aCapo; //nome metodo e tutto il resto string[] messaggioErrore = erroreInterno.Split('@'); string messaggioSenzaSpazi; string[] messaggioInterno; foreach (string messaggio in messaggioErrore) { messaggioSenzaSpazi = messaggio.Trim(); if (messaggioSenzaSpazi != "") { messaggioSenzaSpazi = messaggioSenzaSpazi.Replace(separatore2, "@"); messaggioInterno = messaggioSenzaSpazi.Split('@'); if (messaggioInterno.GetUpperBound(0) == 1) { ritorno += spazio + "metodo: " + messaggioInterno[0] + aCapo; string fileLineaSingolo = messaggioInterno[1].Replace(separatore3, "@"); string[] fileLinea = fileLineaSingolo.Split('@'); ritorno += spazio + "percorsoFile: " + fileLinea[0] + aCapo; ritorno += spazio + "linea: " + fileLinea[1] + aCapo; ritorno += aCapo; } else { ritorno += spazio + "metodo: " + messaggioInterno[0] + aCapo; } } } return ritorno; } private static void ScriviErroreNelDB(int codiceErrore, string descrizioErrore, int localeId, string nomePackage, string descrizionePackage) { int scriviErroreNelDB = int.Parse(ConfigurationManager.AppSettings["scriviErroreNelDB"]); if (scriviErroreNelDB == 1) { //try //{ List parametri = new List(); Parametro parametro = new Parametro(); parametro.DbType = DbType.Int32; parametro.ParameterName = "codiceErrore"; parametro.Value = codiceErrore; parametri.Add(parametro); parametro = new Parametro(); parametro.DbType = DbType.String; parametro.ParameterName = "descrizioErrore"; parametro.Value = descrizioErrore; parametri.Add(parametro); parametro = new Parametro(); parametro.DbType = DbType.DateTime; parametro.ParameterName = "dataTime"; parametro.Value = DateTime.Now; parametri.Add(parametro); parametro = new Parametro(); parametro.DbType = DbType.String; parametro.ParameterName = "localeID"; if (localeId == 0) parametro.Value = System.DBNull.Value; else parametro.Value = localeId; parametri.Add(parametro); parametro = new Parametro(); parametro.DbType = DbType.String; parametro.ParameterName = "descrizionePackage"; parametro.Value = descrizionePackage; parametri.Add(parametro); parametro = new Parametro(); parametro.DbType = DbType.String; parametro.ParameterName = "nomePackage"; parametro.Value = nomePackage; parametri.Add(parametro); DataAccess.ExecuteNonQueryStoredProcedure(DBProvider.SqlServerStampeC6, "[C6Mart].[UT_INSERT_ERROR]", parametri); //} //catch (Exception ex) //{ // DataBaseException dataBaseException = (DataBaseException)ex; // Console.WriteLine(dataBaseException.Message); //} } } private static void StartTng(string ambiente, string cawTo_Bin, string argomentiCaw) { if (!ambiente.Equals("SVILUPPO")) { Process processTng = new Process(); try { processTng = Process.Start(cawTo_Bin, argomentiCaw); while (!processTng.HasExited) ; } catch (Exception ex) { throw new Exception("cawTo_Bin: Non riesco ad eseguire " + cawTo_Bin + " sulla macchina " + System.Environment.MachineName + ":" + ex.Message); } finally { processTng.Close(); processTng.Dispose(); } } } } }