I'd like to create a function app that performs a task that could possibly take 25 to 30 minutes to complete and in some cases that time may be extended.
But after the app completes the task, I'd like to receive an email informing me that the task has been completed.
Is this a doable thing considering the considerable time the task may take to complete and how would email be triggered from within the app?
Seeking a little guidance on this.
You can send a mail from an Azure Function using Microsoft Graph API, SMTP, SendGrid.
Follow below steps to send mail using Microsoft Graph:
ObjectID
of the function app's managed identity to grant permissions. (ObjectIDofMSI)Connect-MgGraph -Scopes "Application.Read.All"
$graphSp = Get-MgServicePrincipal -Filter "displayName eq 'Microsoft Graph'"
$resourceId = $graphSp.Id
Write-Output "Microsoft Graph Service Principal Resource ID: $resourceId"
$mailSendRole = $graphSp.AppRoles | Where-Object { $_.Value -eq "Mail.Send" }
$mailSendRoleId = $mailSendRole.Id
Write-Output "Mail.Send AppRole ID: $mailSendRoleId"
Send.Mail
role to Function App's managed identity using below command:Connect-MgGraph -Scopes Application.Read.All, RoleManagement.ReadWrite.Directory, AppRoleAssignment.ReadWrite.All
$params = @{
principalId = "ObjectIDofMSI"
resourceId = "Service Principal Resource ID" ##ObjectId of Office 365 Exchange Online Enterprise application
appRoleId = "App Role ID" #App permission ID of Exchange.ManageAsApp
}
New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId "ObjectIDofMSI" -BodyParameter $params
Code Snippet to send mail from function app:
Microsoft.Graph
NuGet package to run the below code.using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Microsoft.Graph;
using Azure.Identity;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Graph.Models;
public class Function1
{
private readonly ILogger<Function1> _logger;
public Function1(ILogger<Function1> logger)
{
_logger = logger;
}
[Function("Function1")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
_logger.LogInformation("Starting task...");
await Task.Delay(2000);
_logger.LogInformation("Task completed.");
// Sending Mail
var credential = new DefaultAzureCredential();
var graphClient = new GraphServiceClient(credential, new[] { "https://graph.microsoft.com/.default" });
var message = new Microsoft.Graph.Models.Message
{
Subject = "Azure Function Task Completed",
Body = new Microsoft.Graph.Models.ItemBody
{
ContentType = Microsoft.Graph.Models.BodyType.Text,
Content = "The task triggered by your HTTP request has completed successfully."
},
ToRecipients = new List<Microsoft.Graph.Models.Recipient>
{
new Microsoft.Graph.Models.Recipient
{
EmailAddress = new Microsoft.Graph.Models.EmailAddress
{
Address = "<Receiver_Mail>"
}
}
}
};
var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
{
Message = message
};
try
{
await graphClient.Users["<Sender_Mail>"]
.SendMail
.PostAsync(requestBody);
_logger.LogInformation("Email sent successfully.");
}
catch (ServiceException ex)
{
_logger.LogError($"Error sending email: {ex.Message}");
return new ObjectResult($"Task done, but failed to send email: {ex.Message}") { StatusCode = 500 };
}
return new OkObjectResult("Task completed and email sent.");
}
}
.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.23.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.0.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.2" />
<PackageReference Include="Microsoft.Graph" Version="5.80.0" />
</ItemGroup>
</Project>
Mail Sent: