public virtual MailLogDTO SendMail(Domain.Entity.Client currentClient, MailModel entity, object param) { List <string> recipients = new List <string>(); List <Document> documents = new List <Document>(); MailLog mailLog; ValidateNull(entity); if (!validation.PreValidatePost(validationDictionnary, currentClient, entity, param, repo, recipients, documents)) { throw new ManahostValidationException(validationDictionnary); } IHomeRepository homeRepo = ((MailController.AdditionalRepositories)param).HomeRepo; MailConfig mailconfig = repo.GetMailConfigById(entity.MailConfigId, currentClient.Id); Mailling.SendMailBcc(new InfosMailling(mailconfig.Smtp, (int)mailconfig.SmtpPort, mailconfig.Email, homeRepo.GetHomeById(mailconfig.HomeId, currentClient.Id).Title, Encoding.UTF8.GetString(Convert.FromBase64String(entity.Password))) { body = entity.Body, prio = System.Net.Mail.MailPriority.Normal, subject = entity.Subject, toPeople = recipients, ssl = (bool)mailconfig.IsSSL, attachments = MailUtils.GetAttachments(documents, mailconfig.Home) }, mailLog = new MailLog() { DateSended = DateTime.UtcNow, To = String.Join(",", recipients.ToArray()), Successful = true, HomeId = mailconfig.HomeId }); repo.Add <MailLog>(mailLog); return(GetMapper.Map <MailLog, MailLogDTO>(mailLog)); }
protected override bool ValidatePost(System.Web.Http.ModelBinding.ModelStateDictionary validationDictionary, Client currentClient, MailModel entity, object param, params object[] additionalObjects) { IMailConfigRepository repo = (IMailConfigRepository)additionalObjects[0]; List <string> recipients = (List <string>)additionalObjects[1]; List <Document> attachments = (List <Document>)additionalObjects[2]; MailController.AdditionalRepositories addRepo = (MailController.AdditionalRepositories)param; NullCheckValidation.NullValidation(TypeOfName.GetNameFromType <MailModel>(), new Dictionary <String, Object>() { { "Body", entity.Body }, { "Subject", entity.Subject }, { "Password", entity.Password } }, validationDictionary); if (repo.GetMailConfigById(entity.MailConfigId, currentClient.Id) == null) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <MailModel>(), "MailConfigId"), GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST); } if (entity.To.Count == 0) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <MailModel>(), "To"), GenericError.CANNOT_BE_NULL_OR_EMPTY); } try { Encoding.UTF8.GetString(Convert.FromBase64String(entity.Password)); } catch (Exception) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <MailModel>(), "Password"), GenericError.DOES_NOT_MEET_REQUIREMENTS); } foreach (int cur in entity.To) { People p = addRepo.PeopleRepo.GetPeopleById(cur, currentClient.Id); if (p == null) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <MailModel>(), "To"), GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST); return(false); } if (p.AcceptMailing == true && p.Email != null) { recipients.Add(p.Email); } } if (entity.Attachments != null) { foreach (int cur in entity.Attachments) { Document d = addRepo.DocumentRepo.GetDocumentById(cur, currentClient.Id); if (d == null) { validationDictionary.AddModelError(String.Format(GenericNames.MODEL_STATE_FORMAT, TypeOfName.GetNameFromType <MailModel>(), "Attachments"), GenericError.FORBIDDEN_RESOURCE_OR_DOES_NO_EXIST); return(false); } attachments.Add(d); } } return(validationDictionary.IsValid); }
public override void ProcessDTOPostPut(MailConfigDTO dto, int id, Client currentClient) { orig = repo.GetMailConfigById(dto == null ? id : (int)dto.Id, currentClient.Id); }