MS Graph API Part 6: Send E-Mail

 


In this vlog series I will show you how you can use the Microsoft Graph API  ("Graph API") to manage Azure Active Directory. 

In this sixth epidose I will demonstrate with 2 examples how-to send e-mail message via the Microsoft Graph API. The first is a simple text format e-mail, the second is a more complex HTML message with an attachment.




The  AzureAD_GraphSendEmail.ps1 script  used in the video. Please modify the red parameters to your own values.

# Example file from www.debontonline.com
# Setup Microsoft 365 environment https://developer.microsoft.com/en-us/microsoft-365/dev-program
# Microsoft graph api documentation: https://docs.microsoft.com/en-us/graph/overview?view=graph-rest-1.0
# Base64 encode/decode  https://www.base64encode.org/

# Minimum Required API permission for execution
# Mail.ReadWrite
# Mail.Send

# Required Powershell Module for certificate authorisation
# Install-Module MSAL.PS 


# Connection information for Graph API connection - Certificate Based
$clientID = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx" #  App Id MS Graph API Connector SPN
$TenantName = "<<tenantname>>.onmicrosoft.com" # Example debontonlinedev.onmicrosoft.com
$TenantID = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx" # Tenant ID 
$CertificatePath = "Cert:\LocalMachine\my\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Add the Certificate Path Including Thumbprint here e.g. cert:\currentuser\my\6C1EE1A11F57F2495B57A567211220E0ADD72DC1 >#
##Import Certificate
$Certificate = Get-Item $certificatePath
##Request Token
$TokenResponse = Get-MsalToken -ClientId $ClientId -TenantId $TenantId -ClientCertificate $Certificate
$TokenAccess = $TokenResponse.accesstoken


# Example 1: Send E-mail Message
$MailSenderUPN = "noreply@debontonlinedev.onmicrosoft.com"
$SendMailBody = @{
	Message = @{
		Subject = "Test Message"
		Body = @{
			ContentType = "Text"
			Content =  "This is a test e-mail via Microsoft Graph API!"
		}
		ToRecipients = @(
			@{
				EmailAddress = @{
					Address = "adelev@debontonlinedev.onmicrosoft.com"
				}
			}
		)
		CcRecipients = @(
			@{EmailAddress = @{
					Address = "danas@debontonlinedev.onmicrosoft.com"
			   }
			},
			@{EmailAddress = @{
                    Address = "fannyd@debontonlinedev.onmicrosoft.com"
               }
            }   
		)
	}
}	
$SendMailUrl = "https://graph.microsoft.com/v1.0/users/$MailSenderUPN/SendMail"
$SendMail = Invoke-RestMethod -Uri $SendMailUrl -Headers @{Authorization = "Bearer $($TokenAccess)" }  -Method Post -Body $($SendMailBody | convertto-json -depth 4) -ContentType "application/json"
	
	

# Example 2: Send E-mail Attachment
$MailSenderUPN = "noreply@debontonlinedev.onmicrosoft.com"
$SendMailWithAttachentBody = @{
	Message = @{
		Subject = "HTML Test Message"
		Body = @{
			ContentType = "HTML"
			Content = "This is a test <b>e-mail</b> via Microsoft Graph API!"
		}
		ToRecipients = @(
			@{
				EmailAddress = @{
					Address = "meganb@debontonlinedev.onmicrosoft.com"
				}
			}
		)
		Attachments = @(
			@{
				"@odata.type" = "#microsoft.graph.fileAttachment"
				Name = "attachment.txt"
				ContentType = "text/plain"
				ContentBytes = "SGVsbG8gV29ybGQh"
			}
		)
	}
}
$SendMailWithAttachmentUrl = "https://graph.microsoft.com/v1.0/users/$MailSenderUPN/SendMail"
$SendMailWithAttachment = Invoke-RestMethod -Uri $SendMailWithAttachmentUrl -Headers @{Authorization = "Bearer $($TokenAccess)" }  -Method Post -Body $($SendMailWithAttachentBody  | convertto-json -depth 4) -ContentType "application/json"




Comments

  1. I have read your blog and I got a lot of knowledge from it. I have another article like this one and I have gained a lot of information from that..if you would like to check it I can share the link.
    What Is An API? Understanding The Basics

    ReplyDelete
  2. I am attracted by the info which you have provided in the above post. It is genuinely good and beneficial info for us. Continue posting for Microsoft SWOT analysis, Thank you.

    ReplyDelete

Post a Comment