Exemplo n.º 1
0
        public async Task <bool> SendEmail(EmailMessage message)
        {
            message.Sender = new MailboxAddress(_configuration.SenderName, _configuration.Sender);

            var mimeMessage = CreateMimeMessageFromEmailMessage(message);

            if (mimeMessage == null)
            {
                return(false);
            }

            try
            {
                await _client.ConnectAsync(_configuration.SmtpServer, _configuration.Port, true);

                await _client.AuthenticateAsync(_configuration.UserName, _configuration.Password);

                await _client.SendAsync(mimeMessage);

                await _client.DisconnectAsync(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        private async Task SendEmailAsync(string email, string subject, string body)
        {
            try
            {
                var message = new MimeMessage();
                message.From.Add(new MailboxAddress(smtpSettings.MailSenderName, smtpSettings.MailSender));
                message.To.Add(new MailboxAddress(email));
                message.Subject = subject;
                message.Body    = new TextPart("html")
                {
                    Text = body
                };

                using (smtpClient)
                {
                    await smtpClient.ConnectAsync(smtpSettings.SmtpServer, smtpSettings.Port, SecureSocketOptions.StartTls);

                    await smtpClient.AuthenticateAsync(smtpSettings.Username, smtpSettings.Password);

                    await smtpClient.SendAsync(message);

                    await smtpClient.DisconnectAsync(true);
                }
            }
            catch (Exception e)
            {
                logger.LogError(new EventId(2000, "EmailSendError"), e, "");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Methods for end of
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override async Task StopAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Disconecting from smtp server...");
            await _smtpClient.DisconnectAsync(false, cancellationToken);

            await base.StopAsync(cancellationToken);
        }
Exemplo n.º 4
0
        public async Task SendAsync(MimeMessage message)
        {
            await ConnectSmtpClientAsync();

            await _smtpClient.SendAsync(message);

            await _smtpClient.DisconnectAsync(true);
        }
Exemplo n.º 5
0
        private async Task EnvieEmailAsync(MimeMessage mail)
        {
            try{
                await _smtpClient.ConnectAsync(_configuracaoSmtp.Servidor, _configuracaoSmtp.Porta, _configuracaoSmtp.UseSsl).ConfigureAwait(false);

                if (_configuracaoSmtp.RequerAutenticacao)
                {
                    await _smtpClient.AuthenticateAsync(_configuracaoSmtp.Usuario, _configuracaoSmtp.Senha).ConfigureAwait(false);
                }
                await _smtpClient.SendAsync(mail).ConfigureAwait(false);

                await _smtpClient.DisconnectAsync(true).ConfigureAwait(false);
            }
            catch (Exception ex) {
                throw new EnvioDeEmailException("Erro ao enviar o e-mail.", ex.InnerException);
            }
        }
Exemplo n.º 6
0
        public async Task Send(string to, string subject, string body, bool isHtmlBody = true)
        {
            var email = CreateEmail(to, subject, body, isHtmlBody);

            await _smtpClient.ConnectAsync(_emailSettings.SmtpHost, _emailSettings.SmtpPort, SecureSocketOptions.StartTls);

            await _smtpClient.AuthenticateAsync(_emailSettings.SmtpUser, _emailSettings.SmtpPassword);

            await _smtpClient.SendAsync(email);

            await _smtpClient.DisconnectAsync(true);
        }
Exemplo n.º 7
0
        private async Task ConnectToSmtpServer()
        {
            try
            {
                await _smtpClient.ConnectAsync(_smtpConfiguration.SmtpServer, _smtpConfiguration.SmtpPort);

                if (_smtpClient.Capabilities.HasFlag(SmtpCapabilities.Authentication))
                {
                    await _smtpClient.AuthenticateAsync(_smtpConfiguration.SenderUserName, _smtpConfiguration.SenderPassword);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to connect/authenticate to the SMTP server ({address}:{port})!",
                                 _smtpConfiguration.SmtpServer,
                                 _smtpConfiguration.SmtpPort);

                if (_smtpClient.IsConnected)
                {
                    await _smtpClient.DisconnectAsync(true);
                }
            }
        }
Exemplo n.º 8
0
        public async Task Send(EmailMessage emailMessage, EmailConfiguration emailConfiguration)
        {
            var message = new MimeMessage();

            message.To.AddRange(emailMessage.ToAddresses.Select(x => new MailboxAddress(x.Name, x.Address)));
            message.From.AddRange(emailMessage.FromAddresses.Select(x => new MailboxAddress(x.Name, x.Address)));
            message.Subject = emailMessage.Subject;
            message.Body    = new TextPart(TextFormat.Html)
            {
                Text = emailMessage.Content
            };
            _smtpClient.ServerCertificateValidationCallback = (s, c, h, e) => true;
            await _smtpClient.ConnectAsync(emailConfiguration.SmtpServer, emailConfiguration.SmtpPort, SecureSocketOptions.Auto);

            await _smtpClient.AuthenticateAsync(emailConfiguration.SmtpUsername, emailConfiguration.SmtpPassword);

            await _smtpClient.SendAsync(message);

            await _smtpClient.DisconnectAsync(true);
        }
Exemplo n.º 9
0
        public async Task SendErrorsAsync(IEnumerable <ServiceCheckResultDto> serviceCheckResults)
        {
            Guard.Against.NullOrEmpty(serviceCheckResults, nameof(serviceCheckResults));

            try
            {
                var message = _emailBuilder.BuildUnhealthyServicesEmail(serviceCheckResults,
                                                                        _smtpConfiguration.SenderName,
                                                                        _smtpConfiguration.SenderEmail,
                                                                        _notificationConfiguration.NotificationEmails);

                await _smtpClient.ConnectAsync(_smtpConfiguration.SmtpServer, _smtpConfiguration.SmtpPort);

                await _smtpClient.AuthenticateAsync(_smtpConfiguration.SenderUserName, _smtpConfiguration.SenderPassword);

                await _smtpClient.SendAsync(message);

                await _smtpClient.DisconnectAsync(true);
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to send email about unhealthy services", ex);
            }
        }
Exemplo n.º 10
0
        public async Task SendAsync(string to, string subject, string html, string from = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            // create message
            var email = new MimeMessage();

            email.From.Add(MailboxAddress.Parse(from ?? _appSettings.EmailFrom));
            email.To.Add(MailboxAddress.Parse(to));
            email.Subject = subject;
            email.Body    = new TextPart(TextFormat.Html)
            {
                Text = html
            };

            await _smtpClient.ConnectAsync(_appSettings.SmtpHost, _appSettings.SmtpPort, _appSettings.SmtpTls?SecureSocketOptions.StartTls : SecureSocketOptions.Auto, cancellationToken);

            if (!string.IsNullOrWhiteSpace(_appSettings.SmtpUser))
            {
                await _smtpClient.AuthenticateAsync(_appSettings.SmtpUser, _appSettings.SmtpPass, cancellationToken);
            }

            await _smtpClient.SendAsync(email, cancellationToken);

            await _smtpClient.DisconnectAsync(true, cancellationToken);
        }