/// <summary> /// Executes the service process to check for delivery Confirmation /// </summary> /// <returns>True if process ran OK, False if there was an error</returns> public bool DeliveryServiceWork() { try { string emailAccount = string.Empty; string emailPassword = string.Empty; string emailDomain = string.Empty; SettingsModel model = new SettingsModel(_unitOfWork); string[] emailConfiguration = model.GetEmailConfiguration(); emailAccount = emailConfiguration[0]; emailPassword = emailConfiguration[1]; emailDomain = emailConfiguration[2]; using (mailUtility = new MailUtility(emailAccount, emailPassword, emailDomain)) { List<int> deliveredList = mailUtility.getEmailDeliveryConfirmation(); List<int> nonDeliveredList = mailUtility.GetEmailNonDeliveryConfirmation(); // Gets the list of pendingl emails IList<CS_Email> DeliveredEmails = ListEmailsById(deliveredList); foreach (CS_Email email in DeliveredEmails) { try { UpdateStatusEmail(email, Globals.EmailService.Status.ConfirmationReceived); } catch (Exception ex) { Logger.Write(string.Format("An error has ocurred while trying to update the Email with ID {0}!\n{1}\n{2}", email.ID, ex.InnerException, ex.StackTrace)); } } // Gets the list of nonDelivered emails IList<CS_Email> nonDeliveredEmails = ListEmailsById(nonDeliveredList); foreach (CS_Email email in nonDeliveredEmails) { try { UpdateStatusEmail(email, Globals.EmailService.Status.Error); } catch (Exception ex) { Logger.Write(string.Format("An error has ocurred while trying to update the Email with ID {0}!\n{1}\n{2}", email.ID, ex.InnerException, ex.StackTrace)); } } } } catch (Exception ex) { Logger.Write(string.Format("An error has ocurred while trying to confirm de delivery of the Emails!\n{0}\n{1}", ex.InnerException, ex.StackTrace)); // This process will not send any emails if there's an error, because it might be an exchange server problem return false; } return true; }
/// <summary> /// Generates an Email Record with a status of Pending to be sent by the Email Service /// </summary> /// <param name="receipt">Receipt email address (only one)</param> /// <param name="subject">Email subject</param> /// <param name="body">Email body (HTML)</param> /// <param name="username">Username that requested the email creation</param> /// <param name="employeeId">EmployeeId of the Username that requested the email creation</param> public CS_Email SaveEmail(string receipt, string subject, string body, string username, int? employeeId) { string senderEmail = string.Empty; SettingsModel model = new SettingsModel(_unitOfWork); string[] emailConfiguration = model.GetEmailConfiguration(); senderEmail = string.Format("{0}@{1}", emailConfiguration[0], emailConfiguration[2]); CS_Email email = new CS_Email(); email.Sender = senderEmail; email.Receipts = receipt; email.Subject = subject; email.Body = body; email.Status = (short)Globals.EmailService.Status.Pending; email.StatusDate = DateTime.Now; email.CreatedBy = username; email.CreationID = employeeId; email.CreationDate = DateTime.Now; email.ModifiedBy = username; email.ModificationID = employeeId; email.ModificationDate = DateTime.Now; email.Active = true; return _emailRepository.Add(email); }
/// <summary> /// Executes the service process to send emails /// </summary> /// <returns>True if process ran OK, False if there was an error</returns> public bool EmailServiceWork() { try { string emailAccount = string.Empty; string emailPassword = string.Empty; string emailDomain = string.Empty; SettingsModel model = new SettingsModel(_unitOfWork); string[] emailConfiguration = model.GetEmailConfiguration(); emailAccount = emailConfiguration[0]; emailPassword = emailConfiguration[1]; emailDomain = emailConfiguration[2]; using (mailUtility = new MailUtility(emailAccount, emailPassword, emailDomain)) { // Gets the list of pendingl emails IList<CS_Email> pendingEmails = ListAllPendingEmails(); foreach (CS_Email email in pendingEmails) { try { bool emailed = mailUtility.SendEmail(email.Subject, email.Body, email.Receipts, "", email.ID); if (emailed) UpdateStatusEmail(email, Globals.EmailService.Status.Sent); } catch (Exception ex) { Logger.Write(string.Format("An error has ocurred while trying to send the Email with ID {0}!\n{1}\n{2}", email.ID, ex.InnerException, ex.StackTrace)); UpdateStatusEmail(email, Globals.EmailService.Status.Error); } } } return true; } catch (Exception ex) { Logger.Write(string.Format("An error has ocurred while trying to send the Emails!\n{0}\n{1}", ex.InnerException, ex.StackTrace)); // This process will not send any emails if there's an error, because it might be an exchange server problem return false; } }