Beispiel #1
0
        public void Email(string mailto, string bodyHtmlString, string subject, List <string> cc, string mailFrom = "")
        {
            string     title        = "Nexus Hotel Application";
            var        credUsername = AppConfigUtilities.GetAppConfig <string>("MailServiceUserName");
            var        credPassword = AppConfigUtilities.GetAppConfig <string>("MailServicePassword");
            SmtpClient client       = new SmtpClient();

            client.Credentials = new NetworkCredential(credUsername, credPassword);
            client.Port        = 587;
            client.Host        = "smtp.gmail.com";
            client.EnableSsl   = true;
            MailAddress
                maFrom = new MailAddress(mailFrom.IsNullOrEmpty() ? "*****@*****.**" : mailFrom, title, Encoding.UTF8),
                maTo   = new MailAddress(mailto, title, Encoding.UTF8);

            MailMessage mmsg = new MailMessage(maFrom.Address, maTo.Address);

            if (cc.AnyAndNotNull())
            {
                foreach (string bccEmailId in cc.Where(a => a.IsNullOrEmpty() == false).ToList())
                {
                    mmsg.Bcc.Add(new MailAddress(bccEmailId));
                }
            }
            mmsg.Body            = bodyHtmlString;
            mmsg.BodyEncoding    = Encoding.UTF8;
            mmsg.IsBodyHtml      = true;
            mmsg.Subject         = subject;
            mmsg.SubjectEncoding = Encoding.UTF8;

            client.Send(mmsg);
        }
Beispiel #2
0
        public void WriteLogLocal(string message, string stracktrace = "", string model = "")
        {
            try
            {
                StreamWriter  log;
                FileStream    fileStream = null;
                DirectoryInfo logDirInfo = null;
                FileInfo      logFileInfo;

                string logFilePath = AppConfigUtilities.GetAppConfig <string>("LOG_PATH");;
                logFilePath = logFilePath + "Log-" + DateTime.Now.ToLocalDate().GetDateStrignDDMMMYYYHHMM() + "." + "txt";
                logFileInfo = new FileInfo(logFilePath);
                logDirInfo  = new DirectoryInfo(logFileInfo.DirectoryName);
                if (!logDirInfo.Exists)
                {
                    logDirInfo.Create();
                }
                if (!logFileInfo.Exists)
                {
                    fileStream = logFileInfo.Create();
                }
                else
                {
                    fileStream = new FileStream(logFilePath, FileMode.Append);
                }
                log = new StreamWriter(fileStream);
                log.WriteLine(string.Format("{3}|{0}|{1}|{2}", message, stracktrace, model, DateTime.Now.ToLocalDate().GetDateStrignDDMMMYYYHHMM()));
                log.Close();
            }
            catch (Exception e)
            {
            }
        }
        /// <summary>
        /// Insert Index Object ลง Es เป็นการทำ Indexใหม่ทุกครั้ง
        /// </summary>
        /// <param name="obj"> Object ที่ส่งมาต้อง มี Field Id เป็น Primary Key </param>
        public static void Insert(string json, string indexName)
        {
            try
            {
                var        url    = AppConfigUtilities.GetAppConfig <string>("ElasticsearchUrl");
                HttpClient client = new HttpClient();
                client.Timeout     = new TimeSpan(0, 0, 0, 0, 5000);
                client.BaseAddress = new Uri(url);

                client.DefaultRequestHeaders
                .Accept
                .Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //var json = "{\"id\": \"1\",\"message\": \"hello\"}";
                Console.WriteLine($"LOG:Request  to ES");
                HttpRequestMessage request = new HttpRequestMessage(System.Net.Http.HttpMethod.Post, $"{indexName.ToLower()}/ _doc/");
                request.Content = new StringContent(json,
                                                    Encoding.UTF8,
                                                    "application/json");
                Console.WriteLine($"LOG:Requested  to ES");
                client.SendAsync(request)
                .ContinueWith(responseTask =>
                {
                    Console.WriteLine("Response: {0}", responseTask.Result);
                }).Wait();
                Console.WriteLine($"LOG:{indexName.ToLower()}  to ES");
            }
            catch (Exception e)
            {
                Console.WriteLine($"LOG:Requested  to ES Error-{e.GetMessageError()}");
            }
        }
Beispiel #4
0
 public static string ToImage(this string imageUrl)
 {
     if (!string.IsNullOrEmpty(imageUrl))
     {
         return(imageUrl);
     }
     return(AppConfigUtilities.GetAppConfig <string>("No_IMAGE"));
 }
Beispiel #5
0
        public static string GetDateStringddMMyyyyHHmmssWithLang(this DateTime?datetime)
        {
            if (!datetime.HasValue)
            {
                return(null);
            }

            var culture = AppConfigUtilities.CreateCulture("en");

            return(datetime.Value.ToString("ddMMyyyy HH:mm:ss", culture));
        }
Beispiel #6
0
        public static void SendEmailWithBodyHTML(EmailModel model)
        {
            DomainEvents._Container.Resolve <ILogRepository>().Info(model.ToJSON(), "SendEmailWithBodyHTML", UserUtilities.GetUser(), UserUtilities.GetToken());
            if (model.Email_Tos == null || model.Email_Tos.Any() == false)
            {
                return;
            }
            SmtpClient client = new SmtpClient();

            //client.Port = AppConfigUtilities.GetAppConfig<int>("SMTP_PORT");
            //client.Host = ConfigurationManager.AppSettings["SMTP_HOST"];
            client.Port                  = APPCONSTANT.ConfigSendEmail.SMTP_PORT;
            client.Host                  = APPCONSTANT.ConfigSendEmail.SMTP_HOST;
            client.Timeout               = 10000;
            client.DeliveryMethod        = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            MailMessage msg = new MailMessage();

            msg.From = new MailAddress(AppConfigUtilities.GetAppConfig <string>("FREIGTHX_MAIL_FROM"));
            //var recipients = model.Email_To.Split(';');
            if (model.Email_Tos != null && model.Email_Tos.Any() && model.Email_Tos.Count() > 0)
            {
                foreach (var recipient in model.Email_Tos)
                {
                    msg.To.Add(new MailAddress(recipient));
                }
            }

            if (model.CCs != null && model.CCs.Any() && model.CCs.Count() > 0)
            {
                //var ccsList = model.CCs.Split(';');
                foreach (var cc in model.CCs)
                {
                    msg.CC.Add(new MailAddress(cc));
                }
            }

            msg.IsBodyHtml = true;
            msg.Subject    = model.Subject;
            msg.Body       = model.Body;
            AlternateView alternativeView = AlternateView.CreateAlternateViewFromString(model.Body, null, MediaTypeNames.Text.Html);

            //Add logo
            alternativeView.ContentId        = "htmlView";
            alternativeView.TransferEncoding = TransferEncoding.SevenBit;
            var logo = GetEmbeddedImage("logo", "", "Assets/images/scglogo1.png");

            alternativeView.LinkedResources.Add(logo);
            msg.AlternateViews.Add(alternativeView);
            //End logo

            client.Send(msg);
        }
Beispiel #7
0
        public string UploadFile(byte[] file, string filename)
        {
            var f = new FileUpload();

            f.Id   = Guid.NewGuid().ToString("n");
            f.File = file;
            f.Url  = AppConfigUtilities.GetAppConfig <string>("GatewayUrl") + "user/api/Authentication/GetImage?fileId=" + f.Id;

            f.FileName = f.Id + filename.GetExtension();
            _db.FileUpload.Add(f);
            _db.SaveChanges();
            return(f.Url);
        }
Beispiel #8
0
        public byte[] GetFileUpload(out FileUploadResponseViewModel response, string id)
        {
            response = new FileUploadResponseViewModel();

            var file = _db.FileUpload.Where(s => s.Id == id).FirstOrDefault();


            response.FullName = file.FileName;
            response.Url      = AppConfigUtilities.GetDomain() + "/api/authentication/GetFile?Id=" + id;


            response.Id = file.Id;

            return(file.File);
        }
Beispiel #9
0
 public void Info(string message, string module = "", string CreateBy = "")
 {
     try
     {
         using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
         {
             var log = new LogMessage();
             log.Message    = message;
             log.Level      = EnumLogLevel.Info.ToString();
             log.Module     = module;
             log.CreateBy   = CreateBy;
             log.CreateDate = DateTime.Now;
             log.Domain     = AppConfigUtilities.GetDomain();
             _db.LogMessages.Add(log);
             _db.SaveChanges();
             scope.Complete();
         }
     }
     catch (Exception e)
     {
         WriteLogLocal(message, "", module);
     }
 }
Beispiel #10
0
        public static string GetDateStrignDDMMMYY(this DateTime datetime, EnumLang lang)
        {
            var culture = AppConfigUtilities.CreateCulture(lang.ToString());

            return(datetime.ToString("d MMM yy", culture));
        }
Beispiel #11
0
        public static string GetDateStringForSQLParam(this DateTime datetime)
        {
            var culture = AppConfigUtilities.CreateCulture("en");

            return(datetime.ToString("yyyy-MM-dd HH:mm:ss", culture));
        }
Beispiel #12
0
        public static async Task SendEmailWithBodyHTMLAsync(EmailModel model, bool isChemDailyReport = false)
        {
            DomainEvents._Container.Resolve <ILogRepository>().Info(model.ToJSON(), "SendEmailWithBodyHTMLAsync", UserUtilities.GetUser(), UserUtilities.GetToken());
            if (model.Email_Tos.IsHasValue() == false)
            {
                return;
            }
            SmtpClient client = new SmtpClient();

            //client.Port = AppConfigUtilities.GetAppConfig<int>("SMTP_PORT");
            //client.Host = ConfigurationManager.AppSettings["SMTP_HOST"];
            client.Port                  = APPCONSTANT.ConfigSendEmail.SMTP_PORT;
            client.Host                  = APPCONSTANT.ConfigSendEmail.SMTP_HOST;
            client.Timeout               = 10000;
            client.DeliveryMethod        = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            MailMessage msg = new MailMessage();

            msg.From = new MailAddress(AppConfigUtilities.GetAppConfig <string>("FREIGTHX_MAIL_FROM"));
            //var recipients = model.Email_To.Split(';');
            if (model.Email_Tos.IsHasValue())
            {
                foreach (var recipient in model.Email_Tos.Where(x => string.IsNullOrEmpty(x) == false))
                {
                    msg.To.Add(new MailAddress(recipient));
                }
            }

            if (model.CCs.IsHasValue())
            {
                //var ccsList = model.CCs.Split(';');
                foreach (var cc in model.CCs.Where(x => string.IsNullOrEmpty(x) == false))
                {
                    msg.CC.Add(new MailAddress(cc));
                }
            }

            msg.IsBodyHtml = true;
            msg.Subject    = model.Subject;
            msg.Body       = model.Body;

            #region Send File Or PDF
            if (model.binary != null && model.binary.Any() && !string.IsNullOrEmpty(model.FileName))
            {
                Attachment att = new Attachment(new MemoryStream(model.binary), model.FileName);
                msg.Attachments.Add(att);
            }
            #endregion

            if (isChemDailyReport == false)
            {
                AlternateView alternativeView = AlternateView.CreateAlternateViewFromString(model.Body, null, MediaTypeNames.Text.Html);
                //Add logo
                alternativeView.ContentId        = "htmlView";
                alternativeView.TransferEncoding = TransferEncoding.SevenBit;
                var logo = GetEmbeddedImage("logo", "", "Assets/images/scglogo1.png");
                alternativeView.LinkedResources.Add(logo);
                msg.AlternateViews.Add(alternativeView);
                //End logo
            }

            await client.SendMailAsync(msg);

            //DomainEvents._Container.Resolve<ILogRepository>().Info(msg.ToJSON(), "SendEmailWithBodyHTMLAsync", UserUtilities.GetUser(), UserUtilities.GetToken());
        }