Exemplo n.º 1
0
        public async Task <bool> SendEmail(Application.Models.Email email)
        {
            var client = new SendGridClient(_settings.ApiKey);

            string subject   = email.Subject;
            var    to        = new EmailAddress(email.To);
            string emailBody = email.Body;

            var from = new EmailAddress
            {
                Email = _settings.FromAddress,
                Name  = _settings.FromName
            };

            var sendGridMessage = MailHelper.CreateSingleEmail(from, to, subject, emailBody, emailBody);
            var response        = await client.SendEmailAsync(sendGridMessage);

            _logger.LogInformation("Email sent.");

            if (response.StatusCode == System.Net.HttpStatusCode.Accepted || response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return(true);
            }

            _logger.LogError("Email sending failed.");
            return(false);
        }
        public async Task <bool> SendEmailAsync(Application.Models.Email email)
        {
            var message = new MimeMessage();

            message.From.Add(new MailboxAddress(settings.FromName, settings.FromAddress));
            message.To.Add(MailboxAddress.Parse(email.To));
            message.Subject = email.Subject;

            message.Body = new TextPart("plain")
            {
                Text = email.Body
            };

            using (var client = new SmtpClient())
            {
                client.Connect("smtp.friends.com", 587, false);

                // Note: only needed if the SMTP server requires authentication
                client.Authenticate("joey", "password");

                await client.SendAsync(message);

                client.Disconnect(true);

                logger.LogInformation("Email sent");

                return(true);
            }
        }