Sending Email using Outlook Desktop Application
Introduction
Sending an email from Outlook Desktop client (Outlook Application) is easy. Here we use .NET libraries to open and send the mail using various operations. You can send this mail in plain text or HTML content as we generally do from outlook.
Table styling for mail
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Table styling for mail
$styling = @"
<style>
th, td {
padding: 12px;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
text-align:center;
}
th {
background-color: #008080;
color:white;
}
li {
color:#470FF4;
}
</style>
"@
Mail Content
Mail content is the place where we can keep the HTML content with Intro statements (Headers), Notes (footers) and main content.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$IntroStatement = @"
<p style='margin:0in;font-size:15px;font-family:"Calibri",sans-serif;color:#470FF4;'>Hello Team,</p>
<br/>
<ol style="margin-bottom:0in;margin-top:0in;" start="1" type="1">
<li style='margin-top:0in;margin-right:0in;margin-bottom:0in;margin-left:0in;font-size:15px;font-family:"Calibri",sans-serif;'>Please find <strong>MS EDGE</strong> instances running in the machine currently.</li>
</ol>
<br/>
"@
$Note = "<br/><br/><p style='color:#470FF4;'><i>Note: This is an auto generated Email using PowerShell sent by Ravi Kiran Srikantam. Please reach out to <a href='mailto:scvslsravikiran@tm8h1.onmicrosoft.com'>Ravi Kiran Srikantam</a> for any queries/clarifications. Have a great day!</i></p>"
$Content = $process | ConvertTo-Html -As Table -PreContent "$IntroStatement" -Head $styling -PostContent $Note
$HTMLMailContent = $Content -replace ('<','<') -replace('>','>') -replace (''','')
Create an instance of Outlook.Application COM object
Creating a COM object for Outlook application instance.
1
2
3
4
$outlook = New-Object -ComObject Outlook.Application
# Get the MAPI namespace
$namespace = $outlook.GetNamespace('MAPI')
Construct email item object
Email object to send mails. Here we can configure Subject, Body format, To, Cc, Bcc, sensitivity etc.
1
2
3
4
5
6
7
8
9
10
11
12
$mailItem = $outlook.CreateItem(0)
$mailItem.Subject = "[DatOsmic Team] Processes of MS Edge - $(Get-Date)"
$mailItem.BodyFormat = 2 # HTML Format
$mailItem.HTMLBody = "$($HTMLMailContent)"
$mailItem.To = ""
$mailItem.Cc = ""
$mailItem.Sensitivity = 2
$mailItem.Display()
$mailItem.Save()
# $mailItem.Send() # Uncomment to directly send mail
Use
$mailItem.BodyFormat = 1
for plain text format and$mailItem.BodyFormat = 2
for HTML Format.
Release the COM objects
Releasing objects after completion of sending mail. This is like closing connections after our work.
1
2
3
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($mailItem) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($namespace) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null
Complete code for the automation can be found at Send-OutlookMail.ps1 in my GitHub repository.