static void Main(string[] args) { #if DEBUG pDebug = true; #else pDebug = false; #endif string _myName = System.Reflection.Assembly.GetCallingAssembly().GetName().Name; string _sendTo = "", _subject = "", _carrier = "", _destPath = "", _pureFileName = ""; string _stage = ""; try { // _stage = "Checkings"; Console.WriteLine($"----==== Starting [{_myName}] at {System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} ====----"); string _fileName = args[0].Split("=")[1]; if (_fileName == "") { throw new Exception("Missing absolute file path."); } if (!File.Exists(_fileName)) { throw new Exception($"File not found: {_fileName}"); } _pureFileName = Path.GetFileName(_fileName); // switch (_pureFileName.Substring(_pureFileName.IndexOf("_") + 1, _pureFileName.IndexOf(".") - _pureFileName.IndexOf("_") - 1)) { case "DEDMB": _carrier = "TW"; break; case "AB3CA": _carrier = "ND"; break; case "V9": _carrier = "V9"; break; case "TEST": _carrier = "TEST"; break; default: throw new Exception($"File {_fileName} not recognized"); } _subject = $"Informe de líneas {_carrier}"; _sendTo = (_carrier != "TEST"?$"po_lineas_{_carrier.ToLower()}@grupointerpack.com":"*****@*****.**"); _destPath = $"/media/HISTORICOS/Transmisiones/SAP_REPORT_LINEAS_{_carrier}/"; // _stage = "Connecting to email server"; ExchangeAttachments _email = new ExchangeAttachments(); if (!_email.Connect("processes", "*seso69*", "https://exchange.espackeuro.com/ews/exchange.asmx")) { throw new Exception("Could not connect to email server."); } // _stage = "Sending email"; if (!_email.SendEmail(_sendTo, _subject, "", _fileName)) { throw new Exception("Could not send the email."); } Console.WriteLine($"File {_fileName} sent to {_sendTo}"); // _stage = "Disconnecting"; _email.Dispose(); // _stage = "Copying file"; //File.Move(_fileName, $"{_destPath}{DateTime.Now.ToString("yyyyMMdd_HH.mm.ss")}.{_pureFileName}",true); // this doesn't work: the file is only copied, not removed from the source File.Copy(_fileName, $"{_destPath}{DateTime.Now.ToString("yyyyMMdd-HH.mm.ss")}.{_pureFileName}", true); _stage = "Removing source file"; File.Delete(_fileName); Console.WriteLine($"File {_fileName} moved to {_destPath}"); } catch (Exception ex) { Console.WriteLine($"[Main#{_stage}] {ex.Message}"); } Console.WriteLine($"----==== Ending [{_myName}] at {System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} ====----"); }
public void SendEmail() { string _stage = "", _subject = ""; try { // We send the email if email settings are defined: // - And there was an error // - Or the process worked properly and there are data to show // - Or there are no results, but _noEmpty is false if (ConnDetailsMail != null && !String.IsNullOrEmpty(ConnDetailsMail.Server) && !String.IsNullOrEmpty(ConnDetailsMail.User) && !String.IsNullOrEmpty(ConnDetailsMail.Password)) { if (!NoSend) { // Send errors to informatica if (Error) { Title = "ERROR on " + Title; FileName = null; } // Console.Write($"> Sending {(Error ? "error " : "")}email ({QueryNumber}/{ArgsString})... "); // _stage = "Connecting to email server"; ExchangeAttachments _email = new ExchangeAttachments(); _email.Connect(ConnDetailsMail); // Contents = !String.IsNullOrEmpty(Contents) ? (String.IsNullOrEmpty(FileName)?Contents: "<html><body><b>Message sent automatically.</b><br><i>Mensaje enviado automáticamente.</i></body></html>") : $"<html><body>{(!String.IsNullOrEmpty(EmptyMessage) ? EmptyMessage : "<b>No results found.</b><br><i>No se encontraron resultados.</i>")}</body></html>"; // This is to implement a feature to add the values from parameters in the subject at runtime _subject = Title; if (_subject.Contains("?")) { _stage = "Replacing arguments in subject"; foreach (var _arg in Args) { _subject = _subject.Replace($"?{_arg.Key}", _arg.Value); } } if (!NoExecutionDate) { _subject += $" - Executed on {DateTime.Now.ToString("dd/MM/yyyy")}"; } // _stage = "Sending email"; if (!_email.SendEmail(Error ? MailErrorTo : MailTo, _subject, Contents, FileName)) { throw new Exception("Could not send the email"); } // Console.WriteLine("OK!"); // _stage = "Disconnecting"; _email.Dispose(); } else { // MailSkipped = true; } } } catch (Exception ex) { Console.WriteLine($"[cProcess/SendEmail#{_stage}] {ex.Message}."); } }