Tag Archives: microsoft

Microsoft 365 Mailbox Attachment Processor

A .NET8 C# Application to Process Microsoft 365 Email Messages and Attachments

Introduction

Recently, I was asked how attachments from a Microsoft 365 mailbox could be automatically pushed into an Azure storage file share, so that the attachments can be made accessible to an onward process which needs to be executed on an Azure Virtual Machine. Whilst there are many ways this can be achieved; I decided to create a C# Console application to process the messages and attachments from Microsoft 365 mailbox inbox folder.

You can download this blob post as a PDF here.

Source Code

The source code for this solution can be found in my GitHub repo here.

Dependencies

There are several dependencies for this to work, these are described in the list below.

  • A Microsoft Entra ID registered application, with the following delegated application permissions:
  • An application secret (this can also be a certificate if needed)
  • Install the Microsoft ExchangeOnlineManagement PowerShell tools
  • Create a Microsoft Exchange Online application policy to allow the application access to the mailbox

# Connect to Exchange Online

Connect-ExchangeOnline -UserPrincipalName [Your Exchange Online Admin UPN] [-ShowBanner:$false]

# Create the app policy

New-ApplicationAccessPolicy -AppId [Your application ID] -PolicyScopeGroupId [Full email address of the mailbox] -AccessRight RestrictAccess -Description “Restrict the Mailbox Processor app..”

Reference: Limiting application permissions to specific Exchange Online mailboxes – Microsoft Graph | Microsoft Learn

  • Create an Azure storage account
  • Create an Azure storage account file share

Nuget Packages

The following Nuget packages are a dependency as defined in the project settings.

  <ItemGroup>

    <PackageReference Include=”Azure.Core” Version=”1.44.1″ />

    <PackageReference Include=”Azure.Identity” Version=”1.13.1″ />

    <PackageReference Include=”Azure.Storage.Files.Shares” Version=”12.21.0″ />

    <PackageReference Include=”Microsoft.Extensions.Configuration.Binder” Version=”9.0.0″ />

    <PackageReference Include=”Microsoft.Extensions.Configuration.Json” Version=”9.0.0″ />

    <PackageReference Include=”Microsoft.Graph” Version=”5.63.0″ />

    <PackageReference Include=”Microsoft.Graph.Core” Version=”3.2.1″ />

    <PackageReference Include=”Microsoft.Identity.Client” Version=”4.66.2″ />

  </ItemGroup>

Mailbox Processor Application

The mailbox processor application consists of the following C# Classes and an appsettings.json file.

File NamePurpose
AuthContext.csA C# Class representing the authentication context for the application
JSONConfigurationBuilder.csA C# Class building the configuration from appsettings.json into the application context
MSAzureStorageOperations.csA C# Class with a method to stream the attachment to Azure Storage File Share
MSGraphOperations.csA C# Class with methods to work with the Microsoft Graph API e.g. read/move messages and attachments and folders
Program.csA C# program, the core of the application
Reference.csA C# Class to store the appsettings that are referenced by the application
Appsettings.jsonThe configuration settings for the application

Application Settings

The application settings have been described below.

{

  “AppSettings”: {

    “MailFolderName”: “[The mailbox folder to target to read the messages]”,

    “MailEmailAddress”: “[The mailbox email address]”,

    “MailSubjectSearchString”: “[The subject search string for each mail message]”,

    “ProcessedMessagesFolderName”: “[Process message mailbox folder name]”,

    “AzureStorageConnectionString”: “[The Azure storage connection string] “,

    “AzureStorageFileShareName”: “[Azure storage file share name]”,

    “MSEntraApplicationClientId”: “[Microsoft Entra ID Application Id]”,

    “MSEntraApplicationSecret”: “[Microsoft Entra ID Application Secret]”,

    “MSEntraApplicationTenantId”: “[Microsoft Entra ID Tenant Id]”

  }

}

Application Runtime Process

The application process is described below.

  1. The configuration is initialised
  2. The messages are retrieved from the defined mailbox folder name
  3. Each message is processed in the message collection and the emails with the matched string that are contained in the subject are processed
  4. A console output of the message ID, received date, received from, and subject is displayed
  5. Each attachment is processed and if the file is a file attachment, then the attachment is uploaded to the Azure file share specified in the Azure storage account connection string and file share name
  6. The number of messages processed, and the number of attachments processed is displayed in the output of the console

Sample Output

The mailbox has two messages with the subject containing the search string “course completions”.

The mailbox attachment processor is executed, it displays the following output.

Two messages are processed, although three were seen in the previous email, but since the search string was not contained in the subject, only two messages were processed which were matched.

Two messages are processed, although three were seen in the previous email, but since the search string was not contained in the subject, only two messages were processed which were matched.

Three attachments in total were processed and uploaded to an Azure storage file share.

The email messages were moved to the ProcessedMessages folder, as defined in the application setting ProcessedMessagesFolderName.

When the application is executed again, the output is shown below as there are no longer any matched messages to process.

Closing Thoughts

From a development point of view, using this method provides a simple solution. Other considerations:

  • Store the storage account key in Azure Key Vault
  • Store the application secret (if used( in Azure Key Vault
  • The Azure resource hosting the application e.g. Function App, can have a managed identity and RBAC access can be provided to Azure Key Vault for the service principal (Azure Key Vault access policies are now deprecated)
  • Environment settings can be stored in the hosting environment configuration rather than in the appsettings.json file.

References

Limiting application permissions to specific Exchange Online mailboxes – Microsoft Graph | Microsoft Learn

BLAZOR JARVIS AI – Document Redaction Tool

Welcome to JARVIS, the AI document redaction processor. At the time of publishing this version of his document, I was still developing Jarvis as a fully working product. The current version supports word documents and PDFs. With the development of Blazor Simple AI, I can also utilise the image analysis component to redact PII information from images..

If you would like to download a PDF of this post, you can download it here.

Jarvis is made up of the following technologies:

  • Microsoft .NET Blazor (.NET 6.0 LTS release)
  • Microsoft Azure Cognitive Services (Text Analytics Service)
  • Microsoft Azure Cosmos DB (for maintaining document and redaction processor metadata)
  • Azure Web App (hosting the JARVIS AI Web App)
  • Azure Storage (source document upload and redaction storage)
  • Microsoft Azure Function App (for APIs that process PII data and perform redaction processing)
  • Radzen Blazor components (for an amazing UI experience)

A document named “IPAddressandNamesx10Pages.docx” contains the following information, repeated within 10 pages.

——————————————————————————————————————————-

The IP Address if the legacy system is 10.254.1.1.

The company that owns the legacy system is Microsoft.

The original founders of the company Microsoft are Bill Gates and Paul Allen.

——————————————————————————————————————————-

The document is uploaded to Jarvis, the AI Document redaction processor.

The user clicks “Process” to determine the PII and confidential data held in the document.

A notification is sent to the user to advise the document has been submitted for processing.

About 3 seconds later Jarvis has identified the PII and confidential data in the document and provides a notification to the user.

The user can then click “View” and then select which data needs to be redacted from the document.

The user then clicks “Save choices”. This will save the collection of choices, the metadata, to Azure Cosmos DB.

The user then clicks “Redact” and the user is notified of the submission and completion.

The user clicks the download button which is available after the redaction process has completed. The document is displayed with the information redacted using Microsoft Office apps (this can be downloaded to the machine directly also).

The process is going to be made simpler by a set of walkthroughs in the UI which will be a set of steps with instructions, including a preview document component.

Look out for the next update soon.