public static void SendAll() { lock (_locker) try { HS.GetOrCreateSericeStatus(nameof(MailSender)); using (var unitOfWork = new UnitOfWork(XpoDefault.DataLayer)) { var allMessage2Send = unitOfWork.Query <DoSoEmail>().Where(x => x.Status == BusinessObjects.Base.DoSoMessageBase.MessageStatusEnum.Active && x.ExpiredOn == null && x.SendingDate < DateTime.Now && (x.DoSoReportSchedule == null || x.DoSoReportSchedule.IsActive)); foreach (var message2Send in allMessage2Send) { if (!HS.EnableMailSender) { return; } Send(message2Send, unitOfWork); unitOfWork.CommitChanges(); } } } catch (Exception ex) { HS.GetOrCreateSericeStatus(nameof(MailSender), true); HS.CreateExceptionLog(ex.Message, ex.ToString(), 6); } }
public static void GenerateAll() { lock (_locker) try { HS.GetOrCreateSericeStatus(nameof(SmsGenerator)); using (var unitOfWork = new UnitOfWork(XpoDefault.DataLayer)) { var allSchedule = unitOfWork.Query <DoSoSmsSchedule>().Where(x => x.IsActive && x.NextExecutionDate < DateTime.Now && x.ExpiredOn == null); foreach (var item in allSchedule) { if (!HS.EnableSmsGenerator) { return; } item.GenerateMessages(unitOfWork); item.GetNextExecutionDate(); unitOfWork.CommitChanges(); } } } catch (Exception ex) { HS.GetOrCreateSericeStatus(nameof(SmsGenerator), true); HS.CreateExceptionLog(ex.Message, ex.ToString(), 6); } }
void RunService() { var mailGeneratorThread = new Thread(StartMailGeneratorTread); mailGeneratorThread.SetApartmentState(ApartmentState.STA); var smsGeneratorThread = new Thread(StartSmsGeneratorThread); var emailSenderThred = new Thread(StartEmailSender); var smsSenderThread = new Thread(StartSmsSender); try { while (true) { HS.GetConfiguration(); if (!mailGeneratorThread.CheckThreadState()) { mailGeneratorThread.Start(); } if (!smsGeneratorThread.CheckThreadState()) { smsGeneratorThread.Start(); } if (!emailSenderThred.CheckThreadState()) { emailSenderThred.Start(); } if (!smsSenderThread.CheckThreadState()) { smsSenderThread.Start(); } Thread.Sleep(10000); } } catch (Exception ex) { HS.CreateExceptionLog(ex.Message, ex.ToString(), 10); RunService(); } }
public static void SendAll() { lock (_locker) try { HS.GetOrCreateSericeStatus(nameof(SmsSender)); using (var unitOfWork = new UnitOfWork(XpoDefault.DataLayer)) { var allSmsToSend = unitOfWork.Query <DoSoSms>().Where(x => x.Status == MessageStatusEnum.Active && x.ExpiredOn == null && x.SendingDate < DateTime.Now && (x.DoSoSmsSchedule == null || x.DoSoSmsSchedule.IsActive)); foreach (var smsToSend in allSmsToSend) { Send(smsToSend, unitOfWork); } } } catch (Exception ex) { HS.GetOrCreateSericeStatus(nameof(SmsSender), true); HS.CreateExceptionLog(ex.Message, ex.ToString(), 6); } }
public static void Send(DoSoEmail email, UnitOfWork unitOfWork) { try { using (var mail = new MailMessage()) { try { var fullPath = Path.Combine(email.FolderPath, "HtmlContent"); var strHTML = File.ReadAllText(fullPath); strHTML = strHTML.Replace("HtmlContent" + "_files/", ""); strHTML = strHTML.Replace("src=\"", "src=cid:"); strHTML = strHTML.Replace("gif\"", "gif"); strHTML = strHTML.Replace("png\"", "png"); strHTML = strHTML.Replace("jpg\"", "jpg"); var avHtml = AlternateView.CreateAlternateViewFromString(strHTML, Encoding.Unicode, MediaTypeNames.Text.Html); mail.AlternateViews.Add(avHtml); var di = new DirectoryInfo(fullPath + "_files"); var files = di.GetFiles("*.*"); foreach (var item in files) { var fileMimeType = GetMimeType(item.Name); //using (var pic1 = new LinkedResource(String.Format("{0}\\{1}", di.FullName, item.Name), new ContentType(fileMimeType))) //{ var pic1 = new LinkedResource($"{di.FullName}\\{item.Name}", new ContentType(fileMimeType)) { ContentId = item.Name }; avHtml.LinkedResources.Add(pic1); //} } } catch (Exception) { } if (!string.IsNullOrEmpty(email.SourceFilePath)) { var paths = email.SourceFilePath.Split(';'); foreach (var s in paths) { if (s.Length < 10) { continue; } var attachment = new Attachment(s); mail.Attachments.Add(attachment); } } mail.From = new MailAddress(HS.EMailFrom); var tos = email.EmailTo.Split(';'); foreach (var item in tos.Where(x => x.Length > 2)) { mail.To.Add(new MailAddress(item.Trim())); } var ccs = email?.EmailCC?.Split(';'); if (ccs != null) { foreach (var item in ccs?.Where(x => x.Length > 2)) { mail.CC.Add(new MailAddress(item.Trim())); } } mail.Subject = email.EmailSubject; mail.Body += email.EmailBody; mail.IsBodyHtml = true; var inlineLogo = new Attachment(Path.Combine(email.FolderPath, "Dashboard.png")); mail.Attachments.Add(inlineLogo); inlineLogo.ContentId = "Dashboard"; mail.Body += Environment.NewLine + "<htm><body> <img src=\"cid:" + "Dashboard" + "\"> </body></html>"; using (var client = new SmtpClient(HS.SmtpServer, HS.SmtpPort)) { client.EnableSsl = HS.EnableSsl; //DeliveryMethod = SmtpDeliveryMethod.Network, client.UseDefaultCredentials = HS.UseDefaultCredentials; client.Credentials = new NetworkCredential(HS.SmtpUserName, HS.SmtpPassword); client.Timeout = 50000; client.Send(mail); } email.Status = BusinessObjects.Base.DoSoMessageBase.MessageStatusEnum.Sent; email.SentDate = DateTime.Now; } } catch (Exception ex) { email.CancelMessage($"Exception Was Thrown. See Exception log ({DateTime.Now})", MessageStatusEnum.CancelledByService); HS.CreateExceptionLog(ex.Message, ex.ToString(), 5); } }
public static void Send(DoSoSms smsToSend, UnitOfWork unitOfWork) { try { var smsTo = smsToSend.SmsTo.NormalizeTelNumber(); if (smsToSend.SmsText.Contains("#")) { smsToSend.SmsText = smsToSend.SmsText.Replace("#", "N "); smsToSend.StatusComment = "(# Replaced To N) ;"; } var smsText = smsToSend.SmsText; var url = string.Format(HS.SmsBaseUrl, HS.SmsClientID, smsTo, HS.SmsSenderName, smsText); //http://smsoffice.ge/api/send.aspx?key={0}&destination={1}&sender={2}&content={3} //http://smsoffice.ge/api/send.aspx?key=123456&destination=995577123456&sender=smsoffice&content=TestMessage var request = HttpWebRequest.Create(url); if (request == null) { throw new Exception("SmsSender ->> HttpWebRequest Is Null"); } request.Proxy = null; using (var response = request.GetResponse()) { if (response == null) { throw new Exception("SmsSender ->> Response Is Null"); } using (var reader = new StreamReader(response.GetResponseStream())) { if (reader == null) { throw new Exception("SmsSender - Reader Is Null"); } var urlText = reader.ReadToEnd(); if (urlText?.ToLower()?.Trim() == HS.SmsSuccessCode?.ToLower()?.Trim()) { smsToSend.Status = MessageStatusEnum.Sent; smsToSend.SentDate = DateTime.Now; } else { smsToSend.CancelMessage(string.Format($"Error Code {urlText} Date - {DateTime.Now}"), MessageStatusEnum.CancelledByService); } } } unitOfWork.CommitChanges(); } catch (Exception ex) { smsToSend.CancelMessage($"Exception Was Thrown. See Exception log ({DateTime.Now})", MessageStatusEnum.CancelledByService); HS.CreateExceptionLog(ex.Message, ex.ToString(), 5); } }