Initial support for Graph API E-Mail handler
This commit is contained in:
parent
4c07a777c3
commit
587c21fa03
|
|
@ -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<bool> 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<Recipient>()
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -8,10 +8,16 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="TeamsHook.NET" Version="0.1.0.18" />
|
<PackageReference Include="TeamsHook.NET" Version="0.1.0.18" />
|
||||||
|
<PackageReference Include="Microsoft.Graph" Version="4.51.0" />
|
||||||
|
<PackageReference Include="Azure.Identity" Version="1.8.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\TwilioSMSReceiver.Data\TwilioSMSReceiver.Data.csproj" />
|
<ProjectReference Include="..\TwilioSMSReceiver.Data\TwilioSMSReceiver.Data.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="Microsoft.Graph" />
|
||||||
|
<None Remove="Azure.Identity" />
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
{
|
{
|
||||||
"iisSettings": {
|
"iisSettings": {
|
||||||
"windowsAuthentication": false,
|
"windowsAuthentication": false,
|
||||||
"anonymousAuthentication": true,
|
"anonymousAuthentication": true,
|
||||||
|
|
@ -10,7 +10,6 @@
|
||||||
"profiles": {
|
"profiles": {
|
||||||
"TwilioSMSReceiver.Web": {
|
"TwilioSMSReceiver.Web": {
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"dotnetRunMessages": true,
|
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"applicationUrl": "https://localhost:7213;http://localhost:5213",
|
"applicationUrl": "https://localhost:7213;http://localhost:5213",
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
{
|
{
|
||||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||||
"iisSettings": {
|
"iisSettings": {
|
||||||
"windowsAuthentication": false,
|
"windowsAuthentication": false,
|
||||||
|
|
@ -11,7 +11,6 @@
|
||||||
"profiles": {
|
"profiles": {
|
||||||
"TwilioSMSReceiver": {
|
"TwilioSMSReceiver": {
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"dotnetRunMessages": true,
|
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"launchUrl": "swagger",
|
"launchUrl": "swagger",
|
||||||
"applicationUrl": "https://[::]:7078;http://[::]:5078",
|
"applicationUrl": "https://[::]:7078;http://[::]:5078",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue