public async Task <SendResponse> SendMail(SendMailDTO sendMailDTO) { var mailTemplate = _mailTemplateRepository.GetByIdWithItemsAsync(sendMailDTO.MailTemplateId.ToString()).Result; if (mailTemplate == null) { throw new NotFoundException("Template não encontrado!"); } var template = mailTemplate.Template; foreach (var item in mailTemplate.MailTemplateItems) { if (sendMailDTO.MailTemplateItems.Any(i => i.Key == item.Key)) { template = template.Replace(item.Key, sendMailDTO.MailTemplateItems.Where(i => i.Key == item.Key).FirstOrDefault().Value); } else { template = template.Replace(item.Key, item.Value); } } var response = await _email.SetFrom(mailTemplate.From) .To(sendMailDTO.MailRecipient) .Subject(mailTemplate.Subject ?? "Assunto") .Body(template) .SendAsync(); return(response); }
public ActionResult <bool> Send(SendMailDTO sendMail) { try { ValidationResult result = new SendMailDTOValidator().Validate(sendMail); if (!result.IsValid) { IList <string> erros = new List <string>(); result.Errors.ForAll(s => erros.Add(s.ErrorMessage)); throw new ValidationDTOException(erros); } var res = _mailService.SendMail(sendMail).Result; return(Ok(new SendMailResponseDTO(res.Successful))); } catch (ValidationDTOException ex) { return(BadRequest(new ErrorValidatorDTO(ex.Errors))); } catch (Exception ex) { if ((ex.InnerException ?? ex) is NotFoundException) { return(NotFound(ex.Message)); } return(UnprocessableEntity(ex.Message)); } }
public object[] SendCorreo(SendMailDTO sendMailDTO, List <string> archivos, string nombreCorreo, string nombreArchivo) { MailMessage Mail = new MailMessage(); SmtpClient SMTP = new SmtpClient(); try { Mail.To.Clear(); Mail.From = new MailAddress(sendMailDTO.Remitente, nombreCorreo); foreach (var address in sendMailDTO.Destinatarios.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries)) { Mail.To.Add(address); } Mail.Subject = sendMailDTO.Asunto; Mail.Body = sendMailDTO.Mensaje; Mail.IsBodyHtml = true; Mail.Priority = MailPriority.Normal; if (archivos != null) { //agregado de archivo foreach (string archivo in archivos) { //comprobamos si existe el archivo y lo agregamos a los adjuntos if (System.IO.File.Exists(@archivo)) { var a = new Attachment(@archivo, System.Net.Mime.MediaTypeNames.Application.Octet); Mail.Attachments.Add(a); } } } MailAddress fromAddress = new MailAddress(sendMailDTO.Remitente, nombreCorreo); //CORREO REMITENTE SMTP.Host = Configurations.HOST; // host;//"smtp.gmail.com"; SMTP.Port = Configurations.PUERTO; //587 SMTP.EnableSsl = true; SMTP.DeliveryMethod = SmtpDeliveryMethod.Network; SMTP.UseDefaultCredentials = false; SMTP.Credentials = new NetworkCredential(fromAddress.Address, Configurations.PASSWORD); SMTP.Send(Mail); return(new object[] { true }); } catch (Exception ex) { return(new object[] { false, ex.Message }); } finally { SMTP = null; Mail.Dispose(); Mail = null; } }
public async Task <Result> SendEmail(SendMailDTO dto, IOptions <SMTPOptions> SMTPOptions) { Result result = new Result(); try { var message = new MimeMessage(); //from address message.From.Add(new MailboxAddress(dto.name, SMTPOptions.Value.UserName)); //to address message.To.Add(new MailboxAddress("receiver", SMTPOptions.Value.UserName)); //subject message.Subject = "DDPA Inquiry"; //body string header = "Dear docukit,"; string body = dto.message; string footer = "Regards,\n" + dto.name + ", " + dto.organization + "\n" + dto.email; message.Body = new TextPart("plain") { Text = header + "\n\n" + body + "\n\n\n" + footer }; //cofiguration using (var client = new SmtpClient()) { client.Connect("smtp.gmail.com", SMTPOptions.Value.Port, SMTPOptions.Value.EnableSsl); //aqjx... MAIL-APP password await client.AuthenticateAsync(SMTPOptions.Value.UserName, SMTPOptions.Value.Password); client.Send(message); client.Disconnect(true); result.Message = "Mail has been successfully sent."; result.Success = true; return(result); }; } catch (Exception e) { result.Message = e.Message; result.Success = false; return(result); throw; } }
public async Task <IActionResult> SendMailWithCode(SendMailDTO sendMailDTO) { var id = _tokenHelper.GetIdByToken(HttpContext.Request.Headers["Authorization"]); var isMailSend = await _apiHelper.SendMailWithCode(sendMailDTO.Email, sendMailDTO.FamilyId, id); if (!isMailSend) { return(BadRequest(new { errors = "Niepoprawne id rodziny lub nie jesteś psychologiem." })); } return(Ok(new { message = "Mail został wysłany!" })); }
// POST: api/EmailManager public IHttpActionResult Post([FromBody] SendMailDTO dto) { Thread.Sleep(2000); log.Debug("Invio mail..."); try { var mailingLists = mailingListRepository.Get(dto.IdListeDestinatarie); var destinatari = mailingLists.SelectMany(ml => ml.Emails).Distinct().ToArray(); var email = new Email(new string[0], new string[0], destinatari, dto.Oggetto, dto.Corpo); mailSender.Send(email); } catch (Exception ex) { log.ErrorFormat("L'invio della mail non è stato completato per i seguente errore :\n Error: {0}\n Description: {1}", ex.HResult, ex.Message); throw; } log.Debug("Mail inviate"); return(Ok(new { id = dto.IdListeDestinatarie })); }