/// <summary> /// add email to database /// </summary> /// <returns></returns> public static int AddEmailToDbByEmailModel(int taskId, EmailModel emailModel) { try { using (var db = new FCCEmailAgentEntities()) { EmailContentModel content = new EmailContentModel(emailModel.EmailContentLine); var attachmentsContentModel = content.Elements.Find(x => x.Name == "#attachmentslist#"); Emails email = new Emails(); email.Subject = emailModel.Subject; email.FromEmail = emailModel.FromEmail; email.ReceiverUserId = UsersHelper.CheckExistsUserByEmail(emailModel.ToEmails); email.StateId = 1; email.TaskId = taskId; email.CcResponseTo = string.IsNullOrWhiteSpace(emailModel.CcResponseTo)?"": emailModel.CcResponseTo; email.TypeId = emailModel.TypeId; email.Host = Program.Agent.ServiceSettings.SMTPSettings.Server; email.Port = Program.Agent.ServiceSettings.SMTPSettings.Port; email.Body = EmailGeneratorHelper.GenerateEmailBody(emailModel.TypeId, content.Elements); if (attachmentsContentModel != null) { email.Attachments = EmailGeneratorHelper.GenerateStringAttachments(attachmentsContentModel.Value); } else { email.Attachments = EmailGeneratorHelper.GenerateStringAttachments(""); } db.Emails.Add(email); db.SaveChanges(); EmailTransactionsHelper.AddEmailTransaction(email.Id); return(email.Id); } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); return(-1); } }
/// <summary> /// update email state /// </summary> public static void UpdateEmailState(int emailId, int stateId) { try { using (var db = new FCCEmailAgentEntities()) { Emails email = db.Emails.FirstOrDefault(x => x.Id == emailId); if (email != null) { email.StateId = stateId; db.SaveChanges(); EmailTransactionsHelper.UpdateTransactionStateByEmailState(emailId, stateId); } } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); } }