public string AddDashboardToMail(MailMessage mail, DashboardDefinition dashboard, DoSoDoSoEmailSchedule schedule) { try { var template = dashboard.CreateDashBoard(); var fileName = DateTime.Now.ToString("yyyMMddHHmmssfff"); var tempPath = Path.GetTempPath(); var path = Path.Combine(tempPath, fileName); using (var viewwer = new DashboardViewer()) { var width = Convert.ToInt32(schedule?.DashboardWidth.IsNull(1600)); var height = Convert.ToInt32(schedule?.DashboardHeight.IsNull(1000)); var fileType = schedule?.ExportFileFormat; viewwer.Size = new Size(width, height); viewwer.Dashboard = template; if (fileType == ReportExportFileFormatEnum.HTML) { path += ".png"; viewwer.ExportToImage(path); mail.IsBodyHtml = true; var inlineLogo = new Attachment(path); mail.Attachments.Add(inlineLogo); inlineLogo.ContentId = fileName; mail.Body += Environment.NewLine + "<htm><body> <img src=\"cid:" + fileName + "\"> </body></html>"; Console.WriteLine("Body Assigned"); return(path); } if (fileType == ReportExportFileFormatEnum.PDF) { path += ".pdf"; viewwer.ExportToPdf(path); var attachment = new Attachment(path); mail.Attachments.Add(attachment); return(path); } return(string.Empty); } } catch (Exception ex) { GeneratorHelper.CreateLogFileWithException(ex.ToString()); return(""); } }
static void GetConfiguration(UnitOfWork unitOfWork) { if (ConfigurationWasTaken) { return; } try { SmtpServer = ConfigurationStatic.GetParameterValue(@"DoSoEmailSmtpServer", unitOfWork); SmtpPort = Convert.ToInt32(ConfigurationStatic.GetParameterValue(@"DoSoEmailSmtpPort", unitOfWork)); SmtpEnableSsl = Convert.ToBoolean(ConfigurationStatic.GetParameterValue(@"DoSoEmailEnableSslr", unitOfWork)); UseDefaultCredentials = Convert.ToBoolean(ConfigurationStatic.GetParameterValue(@"DoSoEmailUseDefaultCredentials", unitOfWork)); SmtpUserName = ConfigurationStatic.GetParameterValue(@"DoSoEmailSmtpUserName", unitOfWork); SmtpPassword = ConfigurationStatic.GetParameterValue(@"DoSoEmailSmtpPassword", unitOfWork); MailFrom = ConfigurationStatic.GetParameterValue(@"DoSoEmailMailFrom", unitOfWork); EmailSendingTimeOut = Convert.ToInt32(ConfigurationStatic.GetParameterValue(@"DoSoEmailSendingTimeOut", unitOfWork)); ConfigurationWasTaken = true; } catch (Exception ex) { GeneratorHelper.CreateLogFileWithException(ex.ToString()); } }
static void GetConfiguration(UnitOfWork unitOfWork) { if (ConfigurationWasTaken) { return; } try { //var smsClientUsername = ConfigurationStatic.GetParameterValue(@"DoSoSmsClientUsername", unitOfWork); //var smsClientPassword = ConfigurationStatic.GetParameterValue(@"DoSoSmsClientPassword", unitOfWork); smsClientID = ConfigurationStatic.GetParameterValue(@"DoSoSmsClientID", unitOfWork); //var smsServiceID = ConfigurationStatic.GetParameterValue(@"DoSoSmsServiceID", unitOfWork); SmsSendingTimeOut = Convert.ToInt32(ConfigurationStatic.GetParameterValue(@"DoSoSmsSendingTimeOut", unitOfWork)); smsBaseUrl = ConfigurationStatic.GetParameterValue(@"DoSoSmsBaseUrl", unitOfWork); smsSenderName = ConfigurationStatic.GetParameterValue(@"DoSoSmsSenderName", unitOfWork); smsSuccessCode = ConfigurationStatic.GetParameterValue(@"DoSoSmssmsSuccessCode", unitOfWork); ConfigurationWasTaken = true; } catch (Exception ex) { GeneratorHelper.CreateLogFileWithException(ex.ToString()); } }
public void OnCallBack(Timer timer) { if (executing) { if (EmailSendingTimeOut == 0) { timer.Change(EmailSendingTimeOut, Timeout.Infinite); } return; } executing = true; try { using (var unitOfWork = new UnitOfWork(new SimpleDataLayer(GeneratorHelper.GetConnection()))) { GetConfiguration(unitOfWork); var mailsToSend = unitOfWork.Query <DoSoEmail>().Where(x => !x.IsCanceled && x.ExpiredOn == null && !x.IsSent && x.SendingDate < DateTime.Now); foreach (var mail in mailsToSend) { try { var schedule = mail.DoSoReportSchedule; if (schedule != null) { schedule = unitOfWork.GetObjectByKey <DoSoReportSchedule>(schedule.ID); if (!schedule.IsActive) { continue; } } var reportData = mail.ReportData; if (reportData == null && schedule?.ReportData != null) { reportData = schedule.ReportData; } ReportExportFileFormatEnum?reportType = null; object obj = null; if (reportData != null) { var type = GeneratorHelper.GetMyObjectType(mail.ObjectTypeName); var classInfo = unitOfWork.GetClassInfo(type); var key = classInfo.KeyProperty.Name; obj = unitOfWork.FindObject(classInfo, CriteriaOperator.Parse($"{key} = {mail.ObjectKey}")); reportType = mail.ExportFileFormat; if (reportType == null) { reportType = mail?.DoSoReportSchedule?.ExportFileFormat; } } var isSent = SendMail(mail.EmailTo, mail.EmailCC, mail.SourceFilePath, mail.EmailSubject, mail.EmailBody, reportData, obj, reportType, schedule); if (isSent) { if (!string.IsNullOrEmpty(mail.SourceFilePath)) { var splitedFilePaths = mail.SourceFilePath.Split(';'); foreach (var splitedFilePath in splitedFilePaths) { if (splitedFilePath.Length < 10) { continue; } try { var folderName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SentFiles"); if (!Directory.Exists(folderName)) { Directory.CreateDirectory(folderName); } var newPath = splitedFilePath.Replace("GeneratedFiles", "SentFiles"); File.Move(splitedFilePath, newPath); mail.TargetFilePath += newPath + ";"; } catch (Exception) { /*Ignored*/ } } } mail.SentDate = DateTime.Now; mail.IsSent = true; //mail.DoSoEmailSchedule.GetNextExecutionDate(); unitOfWork.CommitChanges(); } } catch (Exception ex) { GeneratorHelper.CreateLogFileWithException($"Exception On Foreach. ID = {mail.ID}{Environment.NewLine}{ex}"); } } } } catch (Exception ex) { executing = false; GeneratorHelper.CreateLogFileWithException(ex.ToString()); } if (EmailSendingTimeOut > 0) { timer.Change(EmailSendingTimeOut, Timeout.Infinite); } executing = false; }
//private void Viewwer_ConnectionError(object sender, DevExpress.DataAccess.ConnectionErrorEventArgs e) //{ // GeneratorHelper.CreateLogFileWithException(Environment.UserInteractive + Environment.NewLine + e.Exception); //} public bool SendMail(string mailTo, string EmailCC, string path, string subject, string body, ReportData report, object obj, ReportExportFileFormatEnum?fileType, DoSoReportSchedule schedule = null) { //if (report == null) // return false; using (var mail = new MailMessage()) { //var dashboard = schedule?.DashboardDefinition; var otherFilePath = string.Empty; try { if (report != null && obj != null) { otherFilePath = AddAlternateViewWithLinkedResource(mail, report, obj, fileType); } if (!string.IsNullOrEmpty(path)) { var paths = path.Split(';'); foreach (var s in paths) { if (s.Length < 10) { continue; } var attachment = new Attachment(s); mail.Attachments.Add(attachment); } } mail.From = new MailAddress(MailFrom); var tos = mailTo.Split(';'); foreach (var item in tos.Where(x => x.Length > 2)) { mail.To.Add(new MailAddress(item.Trim())); } if (!string.IsNullOrEmpty(EmailCC)) { var ccs = EmailCC.Split(';'); foreach (var item in ccs.Where(x => x.Length > 2)) { mail.CC.Add(new MailAddress(item.Trim())); } } mail.Subject = subject; mail.Body += body; //if (dashboard != null) // try // { // otherFilePath = AddDashboardToMail(mail, dashboard, schedule); // } // catch (Exception ex) // { // throw new InvalidOperationException("Cannot Generate Dashboard" + Environment.NewLine + ex); // } using (var client = new SmtpClient() { Host = SmtpServer, Port = SmtpPort, EnableSsl = SmtpEnableSsl, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = UseDefaultCredentials, Credentials = new NetworkCredential(SmtpUserName, SmtpPassword), Timeout = 50000 }) { client.Send(mail); } DisposeResources(mail); DeleteSentFiles(otherFilePath); return(true); } catch (Exception ex) { //if (!string.IsNullOrEmpty(otherFilePath)) // File.Delete(otherFilePath); DisposeResources(mail); GeneratorHelper.CreateLogFileWithException(ex.ToString()); return(false); } } }
public static void OnCallBack(Timer timer) { if (timer == null) { GeneratorHelper.CreateLogFileWithException("SMS Sender Timer Is Null"); } if (executing) { if (SmsSendingTimeOut > 0) { timer.Change(SmsSendingTimeOut, Timeout.Infinite); } return; } executing = true; try { using (var unitOfWork = new UnitOfWork(new SimpleDataLayer(GeneratorHelper.GetConnection()))) { GetConfiguration(unitOfWork); var allSmsToSend = unitOfWork.Query <DoSoSms>().Where(x => !x.IsSent && !x.IsCanceled && x.ExpiredOn == null && x.SendingDate < DateTime.Now); foreach (var smsToSend in allSmsToSend) { var schedule = smsToSend.DoSoSmsSchedule; if (schedule != null) { if (!schedule.IsActive) { continue; } } var smsTo = NormalizeNumber(smsToSend.SmsTo); if (smsToSend.SmsText.Contains("#")) { smsToSend.SmsText = smsToSend.SmsText.Replace("#", "N "); smsToSend.StatusComment = "# Replaced To N"; } var smsText = smsToSend.SmsText; var url = string.Format(smsBaseUrl, smsClientID, smsTo, 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) { GeneratorHelper.CreateLogFileWithException("DoSoSmsSendingModule - request Is Null"); } request.Proxy = null; using (var response = request.GetResponse()) { if (response == null) { GeneratorHelper.CreateLogFileWithException("DoSoSmsSendingModule - response Is Null"); } var reader = new StreamReader(response.GetResponseStream()); if (reader == null) { GeneratorHelper.CreateLogFileWithException("DoSoSmsSendingModule - reader Is Null"); } var urlText = reader.ReadToEnd(); if (urlText.Trim() == smsSuccessCode.Trim()) { smsToSend.IsSent = true; smsToSend.SentDate = DateTime.Now; } else { smsToSend.IsCanceled = true; smsToSend.StatusComment = string.Format("Error Code {0}", urlText); } } unitOfWork.CommitChanges(); } } } catch (Exception ex) { executing = false; GeneratorHelper.CreateLogFileWithException(ex.ToString()); } if (SmsSendingTimeOut > 0) { timer.Change(SmsSendingTimeOut, Timeout.Infinite); } executing = false; }