Ejemplo n.º 1
0
        public async Task SendEmail(MailModel mailModel)
        {
            try
            {
                var email = new FluentEmail.Core.Email(mailModel.From)
                            .To(mailModel.To)
                            .Subject(mailModel.Subject);
                email.Renderer = new RazorRenderer();

                if (mailModel.TemplateFile != null)
                {
                    email.UsingTemplateFromFile <ViewModel>(mailModel.TemplateFile, mailModel.ViewModel);
                }
                else
                {
                    email.UsingTemplate <ViewModel>(mailModel.Template, mailModel.ViewModel);
                }

                await _smtpSender.SendAsync(email);
            }
            catch (SmtpFailedRecipientException ex)
            {
                _logger.LogError("SmtpMailSender.SendEmail", "SmtpFailedRecipientException occurred while sending mail", new
                {
                    MailModel    = mailModel,
                    SmtpSettings = _smtpSettings,
                    Exception    = ex
                });
            }
            catch (Exception ex)
            {
                _logger.LogError("SmtpMailSender.SendEmail", "Exception occurred while sending mail", new
                {
                    MailModel    = mailModel,
                    SmtpSettings = _smtpSettings,
                    Exception    = ex
                });

                throw;
            }
        }