- 
                Notifications
    
You must be signed in to change notification settings  - Fork 0
 
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
  
- Loading branch information
 
      Showing
      6 changed files
      with
      126 additions
      and
      1 deletion.
    
  
  There are no files selected for viewing
  
    
      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| using Library.Encyclopedia.DataAccess.Email; | ||
| using Microsoft.AspNetCore.Authorization; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.Logging; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.DirectoryServices.AccountManagement; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| 
     | 
||
| namespace Library.Encyclopedia.API.Controllers | ||
| { | ||
| [ApiController] | ||
| [Route("[controller]")] | ||
| public class EmailController : ControllerBase | ||
| { | ||
| public readonly EmailAdapter adapter; | ||
| private readonly ILogger<EmailController> logger; | ||
| 
     | 
||
| public EmailController(ILogger<EmailController> logger, IConfiguration configuration) | ||
| { | ||
| adapter = new EmailAdapter(configuration); | ||
| this.logger = logger; | ||
| } | ||
| 
     | 
||
| [HttpPost] | ||
| [Authorize] | ||
| public async Task<IActionResult> SendEmail([FromBody]string content) | ||
| { | ||
| try | ||
| { | ||
| using (var context = new PrincipalContext(ContextType.Domain | ContextType.Machine)) | ||
| { | ||
| var usr = UserPrincipal.FindByIdentity(context, this.HttpContext.User.Identity.Name); | ||
| await adapter.SendEmail(usr.DisplayName, usr.EmailAddress, content); | ||
| 
     | 
||
| return Ok(); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, $"an error has occured {ex.Message}"); | ||
| throw; | ||
| } | ||
| } | ||
| } | ||
| } | 
  
    
      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using Library.Encyclopedia.Entity.Models; | ||
| using Microsoft.Extensions.Configuration; | ||
| using SendGrid; | ||
| using SendGrid.Helpers.Mail; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.DirectoryServices.AccountManagement; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| 
     | 
||
| namespace Library.Encyclopedia.DataAccess.Email | ||
| { | ||
| public class EmailAdapter | ||
| { | ||
| private readonly IConfiguration configuration; | ||
| 
     | 
||
| public EmailAdapter(IConfiguration configuration) | ||
| { | ||
| this.configuration = configuration; | ||
| } | ||
| 
     | 
||
| public async Task SendEmail(string userName, string email, string content) | ||
| { | ||
| var apiKey = configuration.GetSection("SendGrid-APIKEY").Value; | ||
| var client = new SendGridClient(apiKey); | ||
| var from = new EmailAddress(configuration.GetSection("Admin-Email").Value, configuration.GetSection("Admin-Username").Value); | ||
| 
     | 
||
| var to = new List<EmailAddress>(); | ||
| 
     | 
||
| using (var context = new PrincipalContext(ContextType.Machine)) | ||
| { | ||
| using (var group = GroupPrincipal.FindByIdentity(context, configuration.GetSection("PFW-EmailList-Group").Value)) | ||
| { | ||
| if (group != null) | ||
| { | ||
| var members = group.GetMembers(true); | ||
| foreach (UserPrincipal user in members) | ||
| { | ||
| to.Add(new EmailAddress { Email = user.EmailAddress, Name = user.DisplayName }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| var msg = MailHelper.CreateSingleTemplateEmailToMultipleRecipients(from, to, configuration.GetSection("Template-Id").Value, new EmailPropertiesV1 { Content = content, Name = userName, Email = email }); | ||
| var response = await client.SendEmailAsync(msg); | ||
| 
     | 
||
| if (!response.IsSuccessStatusCode) | ||
| throw new Exception("Email not sent!"); | ||
| } | ||
| } | ||
| } | 
  
    
      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| using Newtonsoft.Json; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| 
     | 
||
| namespace Library.Encyclopedia.Entity.Models | ||
| { | ||
| public class EmailPropertiesV1 | ||
| { | ||
| [JsonProperty("content")] | ||
| public string Content { get; set; } | ||
| [JsonProperty("name")] | ||
| public string Name { get; set; } | ||
| [JsonProperty("email")] | ||
| public string Email { get; set; } | ||
| } | ||
| } |