private static string PopulateParameters(string pStrHtml, LogTypeEnum pEnmType, string pStrMessage) { pStrHtml = pStrHtml.InjectSingleValue("Title", pEnmType.GetDescription()); pStrHtml = pStrHtml.InjectSingleValue("Type", pEnmType.GetDescription()); pStrHtml = pStrHtml.InjectSingleValue("Date", DateTime.Now.ToString("dd' de 'MMMM' de 'yyyy")); pStrHtml = pStrHtml.InjectSingleValue("Time", DateTime.Now.ToString("hh:mm:ss tt")); pStrHtml = pStrHtml.InjectSingleValue("MessageTitle", "Mensaje"); pStrHtml = pStrHtml.InjectSingleValue("Message", pStrMessage); pStrHtml = pStrHtml.InjectSingleValue("Author", Log.Configuration.MailLog.AuthorName); return(pStrHtml); }
private static string[] GetFormattedMessage(LogTypeEnum pEnmType, string pStrMessage) { string lStrFormat = !string.IsNullOrEmpty(Configuration.Log.Format) ? Configuration.Log.Format : "{date} [{type}] {message}"; string lStrDateFormat = !string.IsNullOrEmpty(Configuration.Log.DateFormat) ? Configuration.Log.DateFormat : "yyyy-MM-dd HH:mm:ss"; string lStrFormattedMessage = lStrFormat .InjectSingleValue("date", DateTime.Now.ToString(lStrDateFormat)) .InjectSingleValue("type", pEnmType.GetDescription()) .InjectSingleValue("message", pStrMessage); return(new[] { lStrFormattedMessage }); }
private static void SendMail(LogTypeEnum pEnmType, string pStrMessage) { if (Log.Configuration.MailLog.Active) { MailMessage lObjMailMessage = new MailMessage(); try { // From mail lObjMailMessage.From = new MailAddress(Log.Configuration.MailLog.FromMail, Log.Configuration.MailLog.FromName); string[] lArrToMails = Log.Configuration.MailLog.ToMail.Split(';'); string[] lArrToNames = Log.Configuration.MailLog.ToName.Split(';'); for (int i = 0; i < lArrToMails.Length; i++) { // To mails lObjMailMessage.To.Add(new MailAddress(lArrToMails[i], lArrToNames[i])); } // Content lObjMailMessage.Subject = string.Format("{0} en {1}", pEnmType.GetDescription(), GetAppName()); lObjMailMessage.Body = PopulateParameters ( ObjectExtension.GetTextFromResource <MailLog> ( "AnayaRojo.Tools.Logs.Resources.Templates.DefaultMailTemplate.html" ), pEnmType, pStrMessage ); lObjMailMessage.IsBodyHtml = true; lObjMailMessage.Priority = MailPriority.Normal; // Get mail icon LinkedResource lObjLinkedImage = new LinkedResource(GetIconStream(pEnmType)); lObjLinkedImage.ContentType = new ContentType(MediaTypeNames.Image.Jpeg); lObjLinkedImage.ContentId = "Icon"; // Create view AlternateView lObjHtmlView = AlternateView.CreateAlternateViewFromString ( PopulateParameters ( ObjectExtension.GetTextFromResource <MailLog> ( "AnayaRojo.Tools.Logs.Resources.Templates.CustomMailTemplate.html" ), pEnmType, pStrMessage ), null, "text/html" ); // Add view lObjHtmlView.LinkedResources.Add(lObjLinkedImage); lObjMailMessage.AlternateViews.Add(lObjHtmlView); // Mail account SmtpClient lObjClient = new SmtpClient(); lObjClient.UseDefaultCredentials = false; lObjClient.Credentials = new NetworkCredential(Log.Configuration.MailLog.User, Log.Configuration.MailLog.Password); lObjClient.Host = Log.Configuration.MailLog.Server; lObjClient.Port = Log.Configuration.MailLog.Port; lObjClient.EnableSsl = Log.Configuration.MailLog.EnableSsl; // Send lObjClient.Send(lObjMailMessage); } catch (Exception lObjException) { Log.Write(lObjException); } finally { lObjMailMessage.Dispose(); } } }