Example #1
0
        public void Send(Message messageToSend)
        {
            if (!messageToSend.CanBeSend())
            {
                var exception = new InvalidMessageStateException("Cannot send email with current state");
                exception.Data["MessageId"]    = messageToSend.Id;
                exception.Data["MessageState"] = messageToSend.State;
                throw exception;
            }

            _messageRepository.SetState(messageToSend.Id, MessageState.Sending);

            try
            {
                SendResponse response = _fluentEmail
                                        .SetFrom(messageToSend.From)
                                        .To(messageToSend.To)
                                        .Subject(messageToSend.Subject)
                                        .Body(messageToSend.Body)
                                        .SendAsync().Result;

                UpdateMessageResult(response, messageToSend);
            }
            catch (Exception ex)
            {
                _messageRepository.SetState(messageToSend.Id, MessageState.Failed);
                var exception = new SmtpClientException("Error in SMTP client", ex);
                exception.Data["MessageId"]    = messageToSend.Id;
                exception.Data["MessageState"] = messageToSend.State;
                throw exception;
            }
        }
Example #2
0
        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 async Task <IActionResult> OnPostAsync()
        {
            string userEmail =
                HttpContext.User.FindFirst(JwtRegisteredClaimNames.Email)?.Value;

            Announcement =
                await _apiAnnouncementClient.GetAnnouncementByIdAsync(AnnouncementId);

            AnnouncementUser =
                await _identityClient.GetUserById(Announcement.UserId);

            //Might add a template
            var emailResponse = await _emailSender
                                .SetFrom(userEmail)
                                .To(AnnouncementUser.Email, AnnouncementUser.FirstName)
                                .Subject(EmailSubject)
                                .Body(EmailBody)
                                .SendAsync();

            if (!emailResponse.Successful)
            {
                ModelState.AddModelError("EmailError", "Email could not be sent, try again later.");
                return(Page());
            }

            return(RedirectToPage("/Index"));
        }
Example #4
0
        public void sendProposalEmail(Proposal proposal)
        {
            var email = _email
                        .SetFrom(proposal.Designer.Email)
                        .To(proposal.Customer.Email)
                        .Subject($"Floral Design Proposal for {proposal.Title} 💐")
                        .UsingTemplateFromEmbedded("FinalProject.Views.Proposals.ProposalEmail.cshtml",
                                                   proposal,
                                                   Assembly.Load("FinalProject"));

            email.Send();
        }
 public Task Handle(ISendEmailEvent message)
 {
     return(Task.Run(() =>
     {
         if (!string.IsNullOrWhiteSpace(message.From))
         {
             _client = _client.SetFrom(message.From);
         }
         _client.To(message.To).Subject(message.Subject).UsingTemplateFromEmbedded(
             $"ManageYourBudget.EmailService.Views.{message.Type}.cshtml", message,
             _assembly).Send();
     }));
 }
Example #6
0
        public async Task <IActionResult> SendEmail([FromServices] IFluentEmail email)
        {
            var template = "你好@Model.Name先生, 请核实您的电话号码是否为@Model.Phone";
            var result   = await email//发送人
                           .SetFrom("*****@*****.**")
                           .To("*****@*****.**")
                           .Subject("手机号核实")
                           //传递自定义POCO类
                           //.UsingTemplate<UserInfo>(template, new UserInfo { Name = "张三", Phone吗 = "100110119120" })
                           //或传递匿名对象
                           .UsingTemplate(template, new { Name = "张三", Phone吗 = "100110119120" })
                           .SendAsync();

            return(View());
        }
Example #7
0
        private static IFluentEmail FillFluentEmail(ITemplate template, IFluentEmail fluentEmail, object htmlModel = null, object plainModel = null)
        {
            if (!string.IsNullOrEmpty(template.HtmlBodyTemplate))
            {
                if (htmlModel != null)
                {
                    fluentEmail.UsingTemplate(template.HtmlBodyTemplate, htmlModel);
                }
                else
                {
                    fluentEmail.Body(template.HtmlBodyTemplate, true);
                }
            }

            if (!string.IsNullOrEmpty(template.PlainBodyTemplate))
            {
                if (plainModel != null)
                {
                    fluentEmail.PlaintextAlternativeUsingTemplate(template.PlainBodyTemplate, plainModel);
                }
                else
                {
                    fluentEmail.PlaintextAlternativeBody(template.PlainBodyTemplate);
                }
            }

            if (!string.IsNullOrEmpty(template.Subject))
            {
                fluentEmail.Subject(template.Subject);
            }

            if (!string.IsNullOrEmpty(template.FromAddress))
            {
                fluentEmail.SetFrom(template.FromAddress);
            }

            fluentEmail.Data.Priority = template.Priority;

            if (!string.IsNullOrEmpty(template.Tag))
            {
                fluentEmail.Tag(template.Tag);
            }

            return(fluentEmail);
        }
        /// <inheritdoc/>
        public async Task <SendResponse> SendEmailAsync(EmailMessage message)
        {
            message.ThrowIfNull(nameof(message));

            IFluentEmail email = fluentEmail
                                 .SetFrom(message.From.Address, message.From.DisplayName)
                                 .Subject(message.Subject)
                                 .Body(message.Message);

            message.To?.ForEach(to => email.To(to));
            message.Сс?.ForEach(cc => email.CC(cc));

            email.Data.IsHtml = true;

            logger.LogInformation($"Sending email with title \"{message.Subject}\" asynchronously");

            return(await Policy
                   .Handle <Exception>()
                   .WaitAndRetryForeverAsync(GetSleepTimeForRetry, LogRetryException)
                   .ExecuteAsync(() => email.SendAsync()));
        }