/// <summary> /// Sending An Email with master mail template. /// </summary> /// <param name="mailFrom">Mail From.</param> /// <param name="mailTo">Mail To.</param> /// <param name="mailCC">Mail CC.</param> /// <param name="mailBCC">Mail BCC.</param> /// <param name="subject">Mail Subject.</param> /// <param name="body">Mail Body.</param> /// <param name="languageId">Mail Language.</param> /// <param name="emailType">Email Type.</param> /// <param name="attachment">Mail Attachment.</param> /// <param name="mailLogo">Mail Logo</param> /// <param name="LoginUrl">Login Url</param> /// <param name="attachmentByteList">Attachment byte list for the mail.</param> /// <param name="attachmentName">Attachment file name for the mail.</param> /// <param name="sender">Sender.</param> /// <returns>return send status.</returns> public static bool Send(string mailFrom, string mailTo, string mailCC, string mailBCC, string subject, string body, int languageId, EmailType emailType, string attachment, string mailLogo, string LoginUrl, List <byte[]> attachmentByteList = null, string attachmentName = null, string sender = null) { if (ProjectConfiguration.SkipEmail) { return(true); } if (ProjectConfiguration.IsEmailTest) { mailTo = ProjectConfiguration.FromEmailAddress; mailCC = string.Empty; mailBCC = string.Empty; } ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; if (!string.IsNullOrEmpty(mailFrom)) { mailFrom = mailFrom.Trim(';').Trim(','); } if (!string.IsNullOrEmpty(mailTo)) { mailTo = mailTo.Trim(';').Trim(','); } if (!string.IsNullOrEmpty(mailCC)) { mailCC = mailCC.Trim(';').Trim(','); } if (!string.IsNullOrEmpty(mailBCC)) { mailBCC = mailBCC.Trim(';').Trim(','); } if (ValidateEmail(mailFrom, mailTo) && (string.IsNullOrEmpty(mailCC) || ValidateEmail(mailCC)) && (string.IsNullOrEmpty(mailBCC) || ValidateEmail(mailBCC))) { System.Net.Mail.MailMessage mailMesg = new System.Net.Mail.MailMessage(); mailMesg.From = new System.Net.Mail.MailAddress(mailFrom); if (!string.IsNullOrEmpty(mailTo)) { mailTo = mailTo.Replace(";", ","); mailMesg.To.Add(mailTo); } if (!string.IsNullOrEmpty(mailCC)) { mailCC = mailCC.Replace(";", ","); mailMesg.CC.Add(mailCC); } if (!string.IsNullOrEmpty(mailBCC)) { mailBCC = mailBCC.Replace(";", ","); mailMesg.Bcc.Add(mailBCC); } if (!string.IsNullOrEmpty(attachment) && string.IsNullOrEmpty(attachmentName)) { string[] attachmentArray = attachment.Trim(';').Split(';'); foreach (string attachFile in attachmentArray) { try { System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(attachFile); mailMesg.Attachments.Add(attach); } catch { } } } else if (!string.IsNullOrEmpty(attachment) && !string.IsNullOrEmpty(attachmentName)) { string[] attachmentArray = attachment.Trim(';').Split(';'); string[] attachmentNameArray = attachmentName.Trim(';').Split(';'); if (attachmentArray.Length == attachmentNameArray.Length) { for (int cnt = 0; cnt <= attachmentArray.Length - 1; cnt++) { if (System.IO.File.Exists(attachmentArray[cnt])) { try { string fileName = ConvertTo.String(attachmentName[cnt]); if (!string.IsNullOrEmpty(fileName)) { System.IO.FileStream fs = new System.IO.FileStream(attachmentArray[cnt], System.IO.FileMode.Open, System.IO.FileAccess.Read); System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(fs, fileName); mailMesg.Attachments.Add(attach); } } catch { } } } } } if (attachmentByteList != null && attachmentName != null) { string[] attachmentNameArray = attachmentName.Trim(';').Split(';'); if (attachmentByteList.Count == attachmentNameArray.Length) { for (int cnt = 0; cnt <= attachmentByteList.Count - 1; cnt++) { string fileName = attachmentNameArray[cnt]; if (!string.IsNullOrEmpty(fileName)) { try { MemoryStream ms = new MemoryStream(attachmentByteList[cnt]); System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, fileName); mailMesg.Attachments.Add(attach); } catch { } } } } } mailMesg.Subject = subject; mailMesg.AlternateViews.Add(GetMasterBody(body, subject, emailType, languageId, sender, mailLogo, LoginUrl)); mailMesg.IsBodyHtml = true; System.Net.Mail.SmtpClient objSMTP = new System.Net.Mail.SmtpClient(); try { objSMTP.Send(mailMesg); return(true); } catch (Exception ex) { LogWritter.WriteErrorFile(ex); } finally { objSMTP.Dispose(); mailMesg.Dispose(); mailMesg = null; } } return(false); }
/// <summary> /// AddNotification /// </summary> /// <param name="notificationType">notificationType</param> /// <param name="notificationData">notificationData</param> public static void AddNotification(SystemEnumList.NotificationType notificationType, object notificationData) { try { Notification notification = new Notification() { IsRead = false, NotificationTypeId = (int)notificationType }; bool isAddNotification = false; NotificationDataBL notificationDataBL = new NotificationDataBL(); switch (notificationType) { case SystemEnumList.NotificationType.BookBorrow: case SystemEnumList.NotificationType.BookApprove: case SystemEnumList.NotificationType.BookCancel: case SystemEnumList.NotificationType.BookPending: case SystemEnumList.NotificationType.BookReturnDelay: case SystemEnumList.NotificationType.BookReturnReminder: case SystemEnumList.NotificationType.BookReturnTodayReminder: case SystemEnumList.NotificationType.BookBorrowedByAdmin: case SystemEnumList.NotificationType.BookReturn: BorrowedBook borrowedBook = null; if (notificationData is BorrowedBook) { borrowedBook = (BorrowedBook)notificationData; } else { borrowedBook = notificationDataBL.Search <BorrowedBook>(new BorrowedBook() { ID = (int)notificationData }).FirstOrDefault(); } if (borrowedBook != null && borrowedBook.ID > 0 && borrowedBook.CustomerId > 0) { if (notificationType == SystemEnumList.NotificationType.BookBorrow) { notification.IsAdmin = true; notification.UserId = 0; } else { notification.IsAdmin = false; notification.UserId = borrowedBook.CustomerId; } notification.BorrowedBookId = borrowedBook.ID; notification.NotificationStartDate = DateTime.Now.Date; if (notificationType == SystemEnumList.NotificationType.BookReturnDelay || notificationType == SystemEnumList.NotificationType.BookReturnReminder || notificationType == SystemEnumList.NotificationType.BookReturnTodayReminder) { notification.NotificationEndDate = DateTime.Now.Date; } else { notification.NotificationEndDate = borrowedBook.ReturnDate; } isAddNotification = true; } break; case SystemEnumList.NotificationType.SpaceBooking: case SystemEnumList.NotificationType.SpaceBookingApprove: case SystemEnumList.NotificationType.SpaceBookingReject: case SystemEnumList.NotificationType.SpaceBookingReschedule: SpaceBooking spaceBooking = null; if (notificationData is SpaceBooking) { spaceBooking = (SpaceBooking)notificationData; } else { spaceBooking = notificationDataBL.Search <SpaceBooking>(new SpaceBooking() { ID = (int)notificationData }).FirstOrDefault(); } if (spaceBooking != null && spaceBooking.ID > 0 && spaceBooking.CustomerId > 0) { if (notificationType == SystemEnumList.NotificationType.SpaceBooking) { notification.IsAdmin = true; notification.UserId = 0; } else { notification.IsAdmin = false; notification.UserId = spaceBooking.CustomerId; } notification.SpaceBookingId = spaceBooking.ID; notification.NotificationStartDate = DateTime.Now.Date; notification.NotificationEndDate = spaceBooking.FromDate?.Date.AddDays(1); isAddNotification = true; } break; case SystemEnumList.NotificationType.BookAvailable: BookNotification bookNotification = (BookNotification)notificationData; notification.IsAdmin = false; notification.UserId = bookNotification.CustomerId; notification.BookId = bookNotification.BookId; notification.NotificationStartDate = DateTime.Now.Date; isAddNotification = true; break; default: break; } LogWritter.WriteErrorFile("AddNotification isAddNotification: " + isAddNotification.ToString(), true, "FetchNotifications_"); if (isAddNotification) { var result = notificationDataBL.SaveNotification(notification); LogWritter.WriteErrorFile("AddNotification result: " + result.ToString(), true, "FetchNotifications_"); // if (result > 0) // { // Task.Run(() => // { // new SendNotification().RefreshNotificationCount(SignalRConnections.connections.Values.SelectMany(x => x).ToList()); // }); // } } } catch (Exception ex) { LogWritter.WriteErrorFile("AddNotification Exception: " + ex.ToString(), true, "FetchNotifications_"); } }
/// <summary> /// Handles the Error event of the Application control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void Application_Error(object sender, EventArgs e) { Exception exception = this.Server.GetLastError(); this.Response.Clear(); if (this.IsExceptionIgnored(exception) || exception.Message.ToString().Contains("was not found or does not implement IController.")) { return; } string controller = SmartLibrary.Admin.Pages.Controllers.Error; string action = string.Empty; if (exception.InnerException?.Message == "The remote server returned an error: (404) Not Found.") { action = Actions.UnAuthorizePage; this.Session.Abandon(); this.Session.Clear(); } else if (exception.InnerException?.Message == "The remote server returned an error: (401) Unauthorized.") { action = Actions.UnAuthorizePage; this.Session.Abandon(); this.Session.Clear(); } else { action = Actions.ErrorPage; var httpException = exception as HttpException; if (httpException?.GetHttpCode() == 404) { action = Actions.PageNotFound; } } bool throwError = System.Configuration.ConfigurationManager.AppSettings["ThrowError"].ToBoolean(); if ((!throwError || action == Actions.UnAuthorizePage) && !string.IsNullOrEmpty(action)) { var routeData = new RouteData(); routeData.Values.Add("controller", controller); routeData.Values.Add("action", action); routeData.Values.Add("exception", exception); this.Server.ClearError(); this.Response.TrySkipIisCustomErrors = true; this.Response.Headers.Add("Content-Type", "text/html"); if (controller != SmartLibrary.Admin.Pages.Controllers.Home) { IController errorController = new SmartLibrary.Admin.Controllers.ErrorController(); errorController.Execute(new RequestContext(new HttpContextWrapper(this.Context), routeData)); } else { IController loginController = new SmartLibrary.Admin.Controllers.ErrorController(); loginController.Execute(new RequestContext(new HttpContextWrapper(this.Context), routeData)); } } LogWritter.WriteErrorFile(exception, ProjectSession.Email); }
/// <summary> /// Create notification for return days remaining and return is due. /// </summary> /// <returns>return Status</returns> public static void NotificationsSchedular() { LogWritter.WriteErrorFile("NotificationsSchedular started: ", true, "FetchNotifications_"); NotificationDataBL notificationDataBL = new NotificationDataBL(); var dayCount = ProjectConfiguration.ReturnBookRemainingDayCount > 0 ? ProjectConfiguration.ReturnBookRemainingDayCount : 5; var borrowedBooks = notificationDataBL.Search <BorrowedBook>(new BorrowedBook() { Returned = false, StatusId = SystemEnumList.BorrowBookStatus.Approved.GetHashCode() }).Where(b => b.ReturnDate.HasValue && b.ReturnDate.Value.Date <= DateTime.Now.AddDays(dayCount)).ToList(); LogWritter.WriteErrorFile("NotificationsSchedular borrowedBooks count = " + borrowedBooks.Count.ToString(), true, "FetchNotifications_"); if (borrowedBooks.Count == 0) { return; } List <int> notificationTypes = new List <int>() { SystemEnumList.NotificationType.BookReturnDelay.GetHashCode(), SystemEnumList.NotificationType.BookReturnTodayReminder.GetHashCode(), SystemEnumList.NotificationType.BookReturnReminder.GetHashCode() }; var notificationsExist = notificationDataBL.Search <Notification>(new Notification() { IsAdmin = false, NotificationStartDate = DateTime.Now.Date }).Any(n => n.NotificationTypeId.HasValue && n.NotificationStartDate.HasValue && notificationTypes.Contains(n.NotificationTypeId.Value) && n.NotificationStartDate.Value.Date.Equals(DateTime.Now.Date)); if (notificationsExist) { LogWritter.WriteErrorFile("NotificationsSchedular Notifications Already Exists", true, "FetchNotifications_"); return; } LogWritter.WriteErrorFile("NotificationsSchedular BorrowedBooks Add Notifications Start", true, "FetchNotifications_"); borrowedBooks.ForEach(borrowedBook => { SystemEnumList.NotificationType notificationType = SystemEnumList.NotificationType.BookReturnReminder; EmailViewModel emailModel = new EmailViewModel() { Email = borrowedBook.BorrowerEmail, Name = borrowedBook.CustomerName, BookName = borrowedBook.BookName, Author = borrowedBook.AuthorName, LanguageId = Language.English.GetHashCode(), IsFromJob = true, Date = borrowedBook.ReturnDate.ToDate().ToString(ProjectConfiguration.DateFormat, System.Globalization.CultureInfo.InvariantCulture), OverdueMessage = string.Empty }; if (borrowedBook.ReturnDate.Value.Date.Equals(DateTime.Now.Date)) { notificationType = SystemEnumList.NotificationType.BookReturnTodayReminder; emailModel.OverdueMessage = " , Which has to Return Today"; UserMail.OverdueBookReminderWithDays(emailModel); } else if (borrowedBook.ReturnDate.Value.Date < DateTime.Now.Date) { notificationType = SystemEnumList.NotificationType.BookReturnDelay; UserMail.OverdueBookReminder(emailModel); } else { var daysLeft = (borrowedBook.ReturnDate.Value.Date - DateTime.Now.Date).TotalDays; emailModel.OverdueMessage = ", Which you have to return after " + daysLeft + " days"; UserMail.OverdueBookReminderWithDays(emailModel); } LogWritter.WriteErrorFile($"BorrowedBook details: [CustomerName={borrowedBook.CustomerName}, BookName={borrowedBook.BookName}, ID={borrowedBook.ID}]", true, "FetchNotifications_"); AddNotification(notificationType, borrowedBook); }); LogWritter.WriteErrorFile("NotificationsSchedular ended: ", true, "FetchNotifications_"); }