From 587c21fa0369813696641cff272f58cb554febc7 Mon Sep 17 00:00:00 2001 From: Jeff Leung Date: Tue, 24 Jan 2023 15:43:42 -0800 Subject: [PATCH] Initial support for Graph API E-Mail handler --- .../MessageHandlers/GraphSmtpHandler.cs | 78 +++++++++++++++++++ .../TwilioSMSReceiver.Common.csproj | 6 ++ .../Properties/launchSettings.json | 5 +- .../Properties/launchSettings.json | 5 +- 4 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 TwilioSMSReceiver.Common/MessageHandlers/GraphSmtpHandler.cs diff --git a/TwilioSMSReceiver.Common/MessageHandlers/GraphSmtpHandler.cs b/TwilioSMSReceiver.Common/MessageHandlers/GraphSmtpHandler.cs new file mode 100644 index 0000000..ed4e945 --- /dev/null +++ b/TwilioSMSReceiver.Common/MessageHandlers/GraphSmtpHandler.cs @@ -0,0 +1,78 @@ +using System; +using Azure.Identity; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Microsoft.Graph; +using TwilioSMSReceiver.Common.Interfaces; +using TwilioSMSReceiver.Data.Models; + +namespace TwilioSMSReceiver.Common.MessageHandlers +{ + public class GraphSmtpHandler : IMessageHandler + { + protected readonly ILogger _logger; + protected readonly IConfiguration _configuration; + + protected GraphServiceClient BuildGraphClient() + { + var section = _configuration.GetSection("GraphSmtp"); + var tenantId = section["tenantId"]; + var appId = section["appId"]; + var secret = section["secret"]; + var scopes = new[] { "https://graph.microsoft.com/.default" }; + + var options = new TokenCredentialOptions + { + AuthorityHost = AzureAuthorityHosts.AzurePublicCloud + }; + + var clientSecretCredential = new ClientSecretCredential( + tenantId, appId, secret, options); + + return new GraphServiceClient(clientSecretCredential, scopes); + } + + public GraphSmtpHandler(ILogger logger, IConfiguration configuration) + { + _configuration = configuration; + _logger = logger; + } + + public async Task RelaySms(SMSModel model) + { + var message = new Message + { + Subject = $"Received SMS from {model.SenderNumber} to {model.SenderNumber}", + Body = new ItemBody + { + ContentType = BodyType.Text, + Content = model.MessageContents + }, + ToRecipients = new List() + { + new Recipient + { + EmailAddress = new EmailAddress + { + Address = "frannis@contoso.onmicrosoft.com" + } + } + } + }; + + try + { + var graphClient = BuildGraphClient(); + await graphClient.Me.SendMail(message).Request().PostAsync(); + + return true; + } + catch (Exception e) + { + _logger.LogWarning($"Error occured {e}"); + } + return false; + } + } +} + diff --git a/TwilioSMSReceiver.Common/TwilioSMSReceiver.Common.csproj b/TwilioSMSReceiver.Common/TwilioSMSReceiver.Common.csproj index bf72b9c..8a479d5 100644 --- a/TwilioSMSReceiver.Common/TwilioSMSReceiver.Common.csproj +++ b/TwilioSMSReceiver.Common/TwilioSMSReceiver.Common.csproj @@ -8,10 +8,16 @@ + + + + + + diff --git a/TwilioSMSReceiver.Web/Properties/launchSettings.json b/TwilioSMSReceiver.Web/Properties/launchSettings.json index e7c1d31..d2bfa66 100644 --- a/TwilioSMSReceiver.Web/Properties/launchSettings.json +++ b/TwilioSMSReceiver.Web/Properties/launchSettings.json @@ -1,4 +1,4 @@ -{ +{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, @@ -10,7 +10,6 @@ "profiles": { "TwilioSMSReceiver.Web": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "https://localhost:7213;http://localhost:5213", "environmentVariables": { @@ -25,4 +24,4 @@ } } } -} +} \ No newline at end of file diff --git a/TwilioSMSReceiver/Properties/launchSettings.json b/TwilioSMSReceiver/Properties/launchSettings.json index 603918d..8994550 100644 --- a/TwilioSMSReceiver/Properties/launchSettings.json +++ b/TwilioSMSReceiver/Properties/launchSettings.json @@ -1,4 +1,4 @@ -{ +{ "$schema": "https://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, @@ -11,7 +11,6 @@ "profiles": { "TwilioSMSReceiver": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "https://[::]:7078;http://[::]:5078", @@ -28,4 +27,4 @@ } } } -} +} \ No newline at end of file