protected override void InitializeClientService()
    {
        var smtpClientFactory  = new SmtpClientFactory();
        var mailMessageFactory = new MailMessageFactory();

        _clientService = new EmailClientService(smtpClientFactory, mailMessageFactory)
    }
示例#2
0
        /// <summary>
        /// Asynchronously sends an e-mail on behalf of the given controller using the specified parameters.
        /// </summary>
        /// <param name="controller">The controller that initiated the action.</param>
        /// <param name="model">An object used to create the message to send.</param>
        /// <param name="emailClient">An object used to send the e-mail.</param>
        /// <returns></returns>
        public static async Task <IActionResult> SendEmailAsync(this Controller controller, EmailModel model, IEmailClientService emailClient)
        {
            var config = emailClient.Configuration;

            try
            {
                var message = EmailClientService.CreateMessage(
                    model.Subject,
                    model.Body,
                    model.From,
                    model.To
                    );

                await emailClient.SendAsync(message);

                return(controller.Ok());
            }
            catch (ServiceNotAuthenticatedException ex)
            {
                if (false == config?.RequiresAuth)
                {
                    return(controller.BadRequest(new ServiceNotAuthenticatedException(SmtpServerRequiresAuth)));
                }
                return(controller.BadRequest(ex));
            }
            catch (SslHandshakeException ex)
            {
                if (true == config?.UseSsl)
                {
                    return(controller.BadRequest(new SslHandshakeException(SmtpServerDoesNotSupportSsl)));
                }
                return(controller.BadRequest(ex));
            }
            catch (SocketException)
            {
                return(controller.BadRequest(new Exception(string.Format(SmtpHostUnreachable, config?.Host))));
            }
            catch (Exception ex)
            {
                return(controller.BadRequest(ex));
            }
        }
        async Task RetrieveFailedEmails()
        {
            try
            {
                var emails = await _sysRepo.GetAsync(q => q.Where(s => s.EventType == EMAIL_FAILED).ToArray());

                var firstError = true;

                foreach (var e in emails)
                {
                    try
                    {
                        // deserialize using JSON.NET
                        var m = DeserializeObject <OutgoingMessage>(e.ObjectState);
                        Enqueue(new MimeMessageContainer
                        {
                            UserId    = e.UserId,
                            Message   = EmailClientService.CreateMessage(m.Subject, m.Body, m.From, m.To, messageId: m.MessageId),
                            MessageId = m.MessageId,
                        });
                    }
                    catch (Exception ex)
                    {
                        if (firstError)
                        {
                            Logger.LogWarning(ex, "Error deserializing and creating MIME message from stored e-mail.");
                            firstError = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogCritical(ex, "Could not retrieve the failed stored e-mails.");
            }
        }
示例#4
0
 /// <summary>
 /// Asynchronously sends an e-mail using the specified parameters.
 /// </summary>
 /// <param name="subject">The subject of the message.</param>
 /// <param name="body">The body of the message.</param>
 /// <param name="from">A comma- (or semi-colon) separated list of addresses in the 'From' header.</param>
 /// <param name="to">A comma- (or semi-colon) separated list of addresses in the 'To' header.</param>
 /// <param name="cancellationToken">The token used to cancel an ongoing async operation.</param>
 /// <returns></returns>
 public virtual Task SendEmailAsync(string subject, string body, string from, string to, CancellationToken cancellationToken = default)
 => SendEmailAsync(EmailClientService.CreateMessage(subject, body, from, to), cancellationToken);