Skip to content

Instantly share code, notes, and snippets.

@noahpeltier
Created May 16, 2024 20:34
Show Gist options
  • Save noahpeltier/d3c7baef9ef01a8cab22ef9407572de0 to your computer and use it in GitHub Desktop.
Save noahpeltier/d3c7baef9ef01a8cab22ef9407572de0 to your computer and use it in GitHub Desktop.
graph email browser
# Load the required assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
if (!(get-module Microsoft.Graph.Mail,Microsoft.Graph.Authentication -ListAvailable)) {
Write-host "Hang on, you don't have GraphAPI. Let me install that first" -Foregroundcolor green
Install-Module Microsoft.Graph.Authentication,Microsoft.Graph.Mail -verbos
}
if (!(Get-MgContext)) {
Connect-MgGraph -Scopes Mail.ReadWrite, Mail.ReadWrite.Shared
}
function convertto-GzipURL($string) {
# Get string bytes
$Data = [System.Text.Encoding]::utf8.GetBytes($string)
$MemStream = New-Object IO.MemoryStream
$GZStream = New-Object System.IO.Compression.GZipStream ($MemStream, [Io.Compression.CompressionMode]"Compress")
# Data buffer, Byte offset wich we don't need and Int count
$GZStream.Write($Data, 0, $Data.Length)
$GZStream.Close()
"https://itty.bitty.site/#/data:text/html;charset=utf-8;format=gz,{0}" -f ([Convert]::ToBase64String($MemStream.ToArray()))
$MemStream.Close()
}
# Function to fetch mail folders recursively
function Get-FolderTree {
param (
[string]$UserID,
[string]$ParentID,
[ref]$TreeNode
)
$folders = Get-MgUserMailFolderChildFolder -MailFolderId $ParentID -UserId $UserID -all
foreach ($folder in $folders) {
$node = New-Object System.Windows.Forms.TreeNode
$node.Text = $folder.DisplayName
$node.Tag = $folder.Id
$TreeNode.Value.Nodes.Add($node)
Get-FolderTree -UserID $UserID -ParentID $folder.id -TreeNode ([ref]$node)
}
}
# Function to display messages in a list view
function DisplayMessages {
param (
[string]$UserID,
[string]$FolderID,
[System.Windows.Forms.ListView]$ListView
)
$ListView.Items.Clear()
$Messages = Get-MgUserMailFolderMessage -MailFolderId $FolderID -UserId $UserID -top 50 -ExpandProperty "Attachments"
foreach ($message in $Messages) {
$listItem = New-Object System.Windows.Forms.ListViewItem
$listItem.Text = $message.Subject
$listItem.SubItems.Add($message.From.EmailAddress.Address)
$listItem.SubItems.Add($message.ReceivedDateTime.ToString())
$listItem.Tag = $message
$ListView.Items.Add($listItem)
}
}
# Function to display HTML content in the web browser
function DisplayHTMLContent {
param (
[string]$HTMLContent,
[System.Windows.Forms.WebBrowser]$WebBrowser
)
$base64HTML = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($HTMLContent))
$dataURL = "data:text/html;base64,$base64HTML"
$WebBrowser.Navigate($dataURL)
}
# Create the form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Mailbox Folder Viewer"
$form.Size = New-Object System.Drawing.Size(1280, 720)
# Create the panel for the tree view
$panelTreeView = New-Object System.Windows.Forms.Panel
$panelTreeView.Dock = [System.Windows.Forms.DockStyle]::Left
$panelTreeView.Width = 250
# Create the tree view
$treeView = New-Object System.Windows.Forms.TreeView
$treeView.Dock = [System.Windows.Forms.DockStyle]::Fill
# Add the tree view to the panel
$panelTreeView.Controls.Add($treeView)
# Create the panel for the list view
$panelListView = New-Object System.Windows.Forms.Panel
$panelListView.Dock = [System.Windows.Forms.DockStyle]::Left
$panelListView.Width = 500
# Create the list view
$listView = New-Object System.Windows.Forms.ListView
$listView.Dock = [System.Windows.Forms.DockStyle]::Fill
$listView.View = [System.Windows.Forms.View]::Details
# Add columns to the list view
$listView.Columns.Add("Subject", 300)
$listView.Columns.Add("From", 200)
$listView.Columns.Add("Received Date", 200)
# Add the list view to the panel
$panelListView.Controls.Add($listView)
# Create the panel for the web browser
$panelWebBrowser = New-Object System.Windows.Forms.Panel
$panelWebBrowser.Dock = [System.Windows.Forms.DockStyle]::Fill
# Create the web browser
$webBrowser = New-Object System.Windows.Forms.WebBrowser
$webBrowser.ScriptErrorsSuppressed = $true
$webBrowser.Dock = [System.Windows.Forms.DockStyle]::Fill
# Add the web browser to the panel
$panelWebBrowser.Controls.Add($webBrowser)
# Add the panels to the form
$form.Controls.Add($panelWebBrowser)
$form.Controls.Add($panelListView)
$form.Controls.Add($panelTreeView)
# Event handler for tree view node selection
$treeView.add_AfterSelect({
param ($sender, $e)
$selectedNode = $e.Node
$folderId = $selectedNode.Tag
DisplayMessages -UserID $MyUserID -FolderID $folderId -ListView $listView
})
# Event handler for list view item selection
$listView.add_SelectedIndexChanged({
if ($listView.SelectedItems.Count -gt 0) {
$selectedItem = $listView.SelectedItems[0]
$message = $selectedItem.Tag
$message.Body.content | Out-File .\message.html
$URL = Resolve-Path .\message.html
$webBrowser.Navigate("file://$URL")
#DisplayHTMLContent -HTMLContent $message.Body.Content -WebBrowser $webBrowser
}
})
# Start by fetching the root folders
$MyUserID = (get-aduser $env:USERNAME).userprincipalname
$RootFolders = Get-MgUserMailFolder -UserId $MyUserID -all
foreach ($rootFolder in $RootFolders) {
$rootNode = New-Object System.Windows.Forms.TreeNode
$rootNode.Text = $rootFolder.DisplayName
$rootNode.Tag = $rootFolder.Id
$treeView.Nodes.Add($rootNode)
Get-FolderTree -UserID $MyUserID -ParentID $rootFolder.id -TreeNode ([ref]$rootNode)
}
# Show the form
$form.Add_Shown({$form.Activate()})
[void] $form.ShowDialog()
# Cleanup
$form.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment