Sending an email with attachments using Outlook and PowerShell

A few days ago, I had to find a way to automatically send an email with attachments, at a predetermined time, using Outlook and PowerShell. I decided to use a Windows Scheduled Task, that would wake up at that time and invoke a PowerShell script.

The script below is the result of a collection of snippets and bits of code I found in various places around the Internet that allowed me to solve this problem.

For this example, I only want to send the files which have a specific extension (.html) and that exist in a specific folder (passed as parameter to the PowerShell script). Of course, you are free to remove this extension filtering for your own purposes.

Also, note that Outlook must have a configured email account for the sending process to succeed.

# Check to see we have all the arguments
if($args.Count -lt 1)
Write-Host "Use: SendMail.ps1 <Path>"
Write-Host " <Path>: Full path for the folder which contains the files"


#Get an Outlook application object

$o = New-Object -com Outlook.Application

$mail = $o.CreateItem(0)

#2 = High importance message
$mail.importance = 2

$mail.subject = "This is the subject of the mail"
$mail.body = "This is the body of the email. It has been automatically generated by a script."

#separate multiple recipients with a ";"

# Iterate over all files and only add the ones that have an .html extension
$files = Get-ChildItem $FullPath

for ($i=0; $i -lt $files.Count; $i++) {

$outfileName = $files[$i].FullName
$outfileNameExtension = $files[$i].Extension

# if the extension is the one we want, add to attachments
if($outfileNameExtension -eq ".html")


# give time to send the email
Start-Sleep 20

# quit Outlook

#end the script

You can invoke the script presented above, by using a simple command window and giving the following command:

PowerShell Command

I hope this helps!

What’s New in Azure SErvice Bus? #1

Technical Blogs

Microsoft Azure BizTalk Services: Lessons in Configuring a (Service Bus) Queue Source
An excellent tip by Mark Brimble on how to avoid this trap when integrating Service Bus with MABS.

Azure Service Bus Performance – Http Batch send
One of the easiest ways to increase throughput when using Azure Service Bus is by using the client-side batching feature in the Service Bus protocol (as you can read in our recommendations for increasing throughput). This feature reduces the number of protocol transmissions by batching many messages into one. Note that you will still have many messages in the database.

Service Bus Authentication and Authorization
If you’re working in any MABS development that use the typical LOB Relay pattern, there have been changes associated with the security models for the same.

New Launches/Commercial Information

Microsoft launches Azure cloud in a box
Microsoft is teaming with Dell to create an Azure cloud in a box offering for customers who want to run their own, on-premises datacenters.
Microsoft executives took the wraps off the new offering, known officially as the “Microsoft Cloud Platform System,” during an October 20 event in San Francisco about Microsoft’s cloud futures.

What’s next for Microsoft’s platform as a service?
Microsoft’s Azure team is moving toward remaking the platform as a service part of Azure so that it is more ‘microservice oriented,’ says Mark Russinovich, the Azure Chief Technology Officer

Announcing Azure Event Hubs General Availability
Microsoft Azure’s Event Hubs are the direct result of customer requests and customer feedback, and we are inviting you to try them out today, and to also let us know about your experiences with the product, and the scenarios that you are looking to enable.

Other related products

Microsoft’s fitness band: What kinds of software and services are inside?
We know a lot about Microsoft’s new fitness band, which the company introduced and began selling last week. But what Microsoft didn’t publicly disclose about the Microsoft Band is equally interesting.
Take the operating system inside the Microsoft Band. Although Microsoft did disclose that its band features ARM Cortex M4 MCU processor, the company never mentioned in any of its collateral what the operating system is.

Other mentions

Youth care in the cloud: improving and innovating services
Youth care in the Netherlands is on the verge of major change. QNH’s Bjorn Schmitz discusses how cloud computing can help

AGL jumps on Microsoft Azure Cloud in new Aussie Geo
AGL Energy has jumped on the Azure bandwagon to develop its ‘next generation’ digital platform and is moving its SAP “smartmeter” platform to the new Australian Geo.

Rackspace joins Microsoft’s Windows Azure Pack partner army
Rackspace is now running Microsoft’s Cloud Platform at all of its US data centres.
That platform is a marketing wrapper that contains Hyper-V, System Center and Windows Server with the more recent Azure Pack to spin up and run VM-based clouds.

Automatic Testing with People Picker in SharePoint 2007

I have been working on automatic testing for SharePoint 2007 web applications. In our tests, user interaction with the web interface is simulated by clicking links, selecting entries in combo boxes, typing text and so on, just as a normal user would do.
Initially, it is up to a human user to interact with the web application while an external program records all his steps. If all goes well, it is possible to “play” the recorded steps and thus simulate another interaction.
It is also the automatic test’s responsibility to assert that all the expected conditions are met in the end of the test. These assertions determine if a test has passed or if it has failed. This way, repeating previously run tests to ensure backwards compatibility becomes trivial.

To do this, we’ve been using Telerik’s WebUI Test Studio. Although this tool is not exactly SharePoint-oriented, it still allows us to test our SharePoint components in a very satisfactory way.
This does not mean, however, that we are able to use all the recorded steps in an unmodified way. In fact, sometimes we have to tweak the recorded steps manually in order to select the proper UI element. For instance, sometimes the recording tool will assume that we are clicking on an html div element instead of the table cell that is contained in it. Thus, what we usually do is try to narrow down the search criteria of the automation framework by using specific attributes of that table cell or if all else fails, we convert that particular recorded step into manual C# code, which we can manipulate the way we see fit.

In this case, I was trying to simulate a text insertion into the SharePoint people picker control, but the recording tool was not capturing the events correctly.

People Picker

After exhausting all automatic possibilities (by tweaking the automation framework’s search logic of element attributes) I decided to use manual C# code to solve the problem.

Step 1: I recorded an automatic step, by clicking anywhere on the page. This gave me a step that I could transform into code. Then I converted the recorded step into a coded step.


Convert to code


This creates an automatic C# code file which we can edit to suit our needs. The automatic generated code looks something like this:

[CodedStep(@"Click 'td_73'")]
public void Click_td_73_CodedStep()
    // Click 'td_73'
    HtmlTableCell td_73 = Pages.Home__AutoTests.td_73;

Step 2: In this case, I wanted to simulate the insertion of text into the people picker. Thus, I used WebUI Test Studio’s Overlay Surface feature to correctly identify the element I was trying to manipulate.


Overlay Surface and Dom Explorer


By checking the page’s DOM tree, I was able to determine that the element I was trying to manipulate was an HTML textarea, with a div before it. However, when we try to insert text into the textarea element, nothing happens.

Step 3: After a few tests and after discussing the issue in the Telerik support forums, I was able to work around the problem.

In fact, the element that we want to manipulate isn’t the textarea itself, but the div above it. So, we start by adding the div element to the project Elements pane so we can reference it freely in the C# code.

 Add to Project Elements

Step 4: After that, we must insert the following code into the recorded step that was converted to manual C# code ( the idea is to replace the automatically generated C# code that was presented in Step 1).


[CodedStep(@"Insert into people picker")]
public void InsertItemInPeoplePicker_CodedStep()
    //Note: the div_PlaceHolder__UpLevelDiv object represents the div we want to 
    //manipulate. It was added to the Project Elements in Step 3.
    HtmlControl peoplePicker = Pages.Customize_Workflow.div_PlaceHolder__UpLevelDiv;
    Manager.Desktop.KeyBoard.TypeText("testDeveloper", 200);


After the replacement of the automatic code with the code presented above, it is possible to insert the text “testDeveloper” into the people picker. And that’s it.

Hope this helps.