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")} ====----"); }
static void Main(string[] args) { #if DEBUG pDebug = true; #else pDebug = false; #endif string _stage; string _currentArgName, _currentArgValue; string _user = "", _password = "", _path = "", _subject = "", _filefilter = "", _sender = "", _reportTo = ""; string _separator = pDebug ? "\\" : "/"; // Args _stage = "Checking args"; try { foreach (string arg in args) { // Get the arg name and value _currentArgName = arg.Split('=')[0].ToUpper(); if (arg.Split('=').Length == 2) { _currentArgValue = arg.Split('=')[1]; } else if (arg.Split('=').Length > 2) { throw new Exception($"Wrong argument: {arg}"); } else { _currentArgValue = ""; } // Identify arg name switch (_currentArgName.ToUpper()) { case "USER": _user = _currentArgValue; break; case "PASSWORD": _password = _currentArgValue; break; case "PATH": _path = _currentArgValue; if (_path.Substring(_path.Length - _separator.Length) != _separator) { _path = _path + _separator; } break; case "SUBJECT": _subject = _currentArgValue; break; case "FILEFILTER": _filefilter = _currentArgValue; break; case "SENDER": _sender = _currentArgValue; break; case "REPORTTO": _reportTo = _currentArgValue; if (!Regex.IsMatch(_reportTo, @".*@.*\..*")) { throw new Exception($"Wrong email address: {_reportTo}"); } break; default: throw new Exception($"Wrong argument: {_currentArgName}"); } } } catch (Exception ex) { Console.WriteLine($"[Main#{_stage}] {ex.Message}"); return; } Console.WriteLine($"----==== Starting Exchange Attachment Checking Process at {System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} ====----"); Console.WriteLine($"> Parameters: {String.Join(" ",args) }"); if (_user == "" || _password == "" || _path == "") { Console.WriteLine($"> ERROR at {_stage}: USER, PASSWORD and PATH are mandatory."); } else { //Console.WriteLine("CACA"); using (var _exchange = new ExchangeAttachments()) { try { // _stage = "Creating connection"; _exchange.Connect(_user, _password, @"https://exchange.espackeuro.com/ews/exchange.asmx"); // _stage = "Looking for matching emails"; if (_exchange.DownloadFromExchange(_path, _subject, _filefilter, _sender) && _reportTo != "") { // _stage = "Sending report"; _exchange.SendEmail(_reportTo); Console.WriteLine($" -> Report sent to {_reportTo}"); } } catch (Exception ex) { Console.WriteLine($"> ERROR at {_stage}: {ex.Message}"); } } } Console.WriteLine($"----==== Ending Exchange Attachment Checking Process 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}."); } }