Пример #1
0
        public async Task <IActionResult> SendEmail([FromBody] Email email)
        {
            if (!ModelState.IsValid)
            {
                loggerService.Log("SendContactEmailAction - ModelState invalid");
                loggerService.Log($"Email address: {email.EmailAddress}, Subject: {email.Subject}" +
                                  $", Message: {email.Message}, Description: {email.Description}, Username: {email.Username}");
                loggerService.Log(ModelState.Values.ToString());
                return(BadRequest(ModelState));
            }

            var emailSettings     = emailSettingsService.GetSettings();
            var contactEmailToMod = new Email
            {
                EmailAddress = emailSettings.EmailAddress,
                Message      = emailMessageLayoutService.ContactModMessage(email),
                Subject      = $" [{ConstStrings.Servicename}] - temat: {email.Subject}",
                Description  = email.Description,
                Username     = email.Username
            };

            await emailService.SendEmailAsync(contactEmailToMod);

            var confirmEmailToUser = new Email
            {
                EmailAddress = email.EmailAddress,
                Message      = emailMessageLayoutService.ContactUserFeedback(email.Username, ConstStrings.Servicename),
                Subject      = $"[{ConstStrings.Servicename}] - temat: {email.Subject}"
            };
            await emailService.SendEmailAsync(confirmEmailToUser);

            return(Ok(true));
        }
Пример #2
0
 public async Task SendEmailAsync(Email email)
 {
     try
     {
         var emailSettings = emailSettingsService.GetSettings();
         using (var mailMessage = new MailMessage(emailSettings.EmailAddress, email.EmailAddress))
         {
             mailMessage.Subject    = email.Subject;
             mailMessage.Body       = email.Message;
             mailMessage.IsBodyHtml = emailSettings.IsHtmlMessage;
             using (var smtpClient = new SmtpClient())
             {
                 smtpClient.Host        = emailSettings.Smtp;
                 smtpClient.Port        = emailSettings.Port;
                 smtpClient.Credentials =
                     new NetworkCredential(emailSettings.Username, emailSettings.Password);
                 smtpClient.EnableSsl = emailSettings.EnableSsl;
                 await Task.Run(() => smtpClient.Send(mailMessage));
             }
         };
     }
     catch (Exception e)
     {
         loggerService.Log($"Send email exception - {e.Message}");
     }
 }