PowerShell Automation: How To Send Email with HTML Body Containing Inline Images

During of a recent assignment I get across an automation requirement to send formatted emails daily to a set of users.

The email content involved the HTML formatting with lot of images as a part of layout. I have appointed PowerShell as automation technology to get this done.

I found worth writing a small article to explain the code (which is interesting) to embed images with in outgoing email content.

Now to start with demo, say we have to send an email with Health of Weekly Sales Card for an online shopping store every week on Monday.

This email would look like the one in the following screen shot:


In Step 1,2,4 we are defining HTML that will be send as Email Content.

Here it is very important to notice the “SRC” Tag where we are specifying image path not as actual path but as a Content Identifier that is mapped to the path of the attachment added to the Email.

And this is the trick that you have to remember to get the images embedded into the html body of the email.


Now once we are done with defining body content for Email Body, we can start configuring SMTP Client and adding the attachments to the email

In Step 5 we will initialize “SMTP Server”, “Mail Message”, and “SMTP Client” Objects

In Step 6 configure “From” & “To” Email IDs for the intended recipients. Also specify the “Subject” & “Body” for the email


In Step 7 & 8 we will add attachments to the Email and provide each attachment a Content Identifier (remember we used this identifier in “SRC” Tag while defining HTML content for the body)

Attachments can be added to the email by using object of “Attachment” Class.

While initializing the “Attachment” Object we have to provide actual path of the attachment as input parameter

Then we have to make sure that

  1. “Inline” property should be set to “True”,
  2. “DispositionType” property should be set to “Inline”,
  3. “MediaType” property should be set to “<type of content, you are attaching>”,
  4. “ContentId” property should be set to any unique Content Identifier to represent the attachment uniquely


In Step 9 we will send the email by call in “Send” method of SMTP Client object. Make sure to dispose the “Attachments” & “Mail Message” objects


And finally we will call the Send-Email Function in Step 10 to run all the code snippets described earlier


That is all for this demo.

Hope you find it helpful.



PowerShell Automation: How to Send Email Notifications

While developing some of the automation tools I was required to send emails to the intended users sometimes to inform them about the operational progress or otherwise.

In this article I will take you through the steps to develop PowerShell functions that can send emails to the intended participants.

Let’s look into the code base used for this article-

Step – 1: This will be the final step where we are calling the “Send-Email” function, implementation of which can be seen in the upcoming steps.

Step – 2: In this step I have shown the distribution list comprising of intended participants, here in this article I choose to make it hard coded with in the code which is an absolutely bad idea J and should be avoided. Rather we should have external content source (CSVs, SharePoint Lists) to configure this list of intended recipients.

Step – 3: In this step we have declared a variable holding subject for the email

Step – 4: In this step we prepare the body of the email. It is worth noting that you can use any valid HTML to prepare the mail body. Make sure you don’t use CSS Classes as they won’t work here. You can use inline styles to format the html.

Step – 5: In this step we will call the helper function and pass on the data we have prepared in previous steps. This function has the responsibility to send the email to the required recipients.



Step – 6: In this step we get the Sender’s Id. As a part of the best practice this should be a generic Id.

Step – 7: In this we get SMTP Server Address to relay through the mail

Step – 8: In this step we are creating the array of recipient since “Send-MailMessage” function uses Array as parameter return type to “-To” parameter.

Step – 9: In this step we are calling “Send-MailMessage” function by passing required parameters as shown below-


That’s all for the code.

On successful execution we will get a new email in the Inbox as shown below-


That is all for this simple demo.

Hope you find it helpful.

SharePoint Online/2016/2013: How To Upload Large Files Using PowerShell Automation

Uploading large files to SharePoint On-Premise or Online is an obvious problem during data migration from any external systems like Lotus Notes.

Here is one of such errors which we might encounter while trying to upload a file of size greater than 250 MB-


In this article I will explain a data upload strategy where we can split a large file into multiple chunks of smaller size.

Solution Architecture Diagram

For better understanding we can refer to the following solution architecture diagram-


Based on this diagram we can conclude the following facts-
1. This solution can be hosted on multiple servers to launch parallel uploads
2. This solution can consume data from Network File Shares
3. Once data file is retrieved (say of size 300 MB), this solution will split the file (100 MB) automatically based on the pre-configured chunk size (which should not exceed the size limit of 250 MB)
4. Each chunk then appended to the file uploaded in multiple iterations

In order to start with this demo we would need a SharePoint Document Library in SharePoint Online (or On-Premise) Site as shown below-


Another prerequisite to demo is to have files of various sizes that we can use to upload to the document library.

I made use of following command line utility to generate files of various sizes. This utility takes destination folder path and size of the file in KBs as input.

Here is the usage example of the command line utility-

fsutil file createnew "C:\Prashant\Self Paced Training\Sample Files\2GB.txt" 2147483648

Similarly I have generated other files too as shown below-


Now let’s dive down into the code to understand the actual implementation.

Step 1: Declare a variables to hold the document library name & folder path. For production use I recommend to have these values in an external configuration file.

Step 2: Reading files from the folder specified in path variable in Step 1

Step 3: Loop through all the files and pass each file to the “UploadLargeFiles” function along with the document library name


Step 4: Generate unique upload id & get file name of the file to be uploaded

Step 5: Get handle on document library object and load the root folder (or any target folder) with in the document library

Step 6: Calculate the block size to be uploaded and total file size (as shown in the architecture diagram)

Step 7: Read the bytes from the source file and set the read buffer based on the block size


Step 8: Read the bytes based on the buffer limit that we set in earlier steps


Step 9: Check if this is the first chunk that is being uploaded, if yes then add a new file to SharePoint Document Library, get the file content based on the buffer size for the chunk and call “StartUpload” function that is defined under “Microsoft.SharePoint.Client.File” class. This will add the file to the document library but with small bunch of content only.

Step 10: Check if this is not the first chunk that is being uploaded, if yes then find the file in document library and get the handle on it

Step 11: If this is another chunk of data which is not the last chunk, this chunk will be appended to the same file by using “ContinueUpload” function that is defined under “Microsoft.SharePoint.Client.File” class. This will append the content to the file identified by Upload Id that we have initialized in earlier steps.

Step 12: If this is last chunk of data, this chunk will be appended to the same file by using “FinishUpload” function that is defined under “Microsoft.SharePoint.Client.File” class. This will append the content to the file identified by Upload Id that we have initialized in earlier steps and commits the changes to the file. Once this function completes successfully the changes will be made persistent to the file.


Step 13: Perform exception handling and call the “UploadLargeFileToLibrary”


I recommend to read the documentation on Microsoft.SharePoint.Client.File class and understand functions carefully before using it.

Once we execute this script we can see the following information-

  1. File Name to be uploaded
  2. Chunk size
  3. Total Time taken to upload the files

It is important to note that total time taken to upload the files may vary depending on the hosting environment.

File Size to be uploaded: 10 MB


File Size to be uploaded: 50 MB


File Size to be uploaded: 500 MB


File Size to be uploaded: 2 GB


Once the script executed successfully we can see the respective files uploaded to the SharePoint Online Site as shown below-


That is all for this demo.

This article is equally applicable for both SharePoint Online & On-Premise Versions.

Hope you find it helpful.