I had to learn a few tricks along the way, and thought it worth documenting it here.
The scripts form the basis of a replacement for Microsoft's IIS 6 SMTP server.
Using Amazon SES
First ensure SES is properly configured for your domain. This involves the usual proving you own the domain etc., as well as requesting a removal of the limit that allows you initially to only test it.Once SES is set up, you set up SNS to monitor the sending. The simplest way is to simply email SNS notifications to a dedicated mailbox where you can then search for specific email addresses on demand to see if emails were delivered, bounced or rejected.
Of course, you can set up something much more clever with SNS given time and inclination.
The work-horse of this method is Send-SESRawEmail. For me the problem was that Send-SESRawEmail takes a MemoryStream as input, and I struggled to find the documentation on how to do this. I'm not sure how efficient it is, but this script takes a file and converts it to a MemoryStream before passing it in.
Using SMTP
It is possible to send an email directly to an SMTP server. I’ve not tried encrypted SMTP yet. This obviously requires an SMTP server to be set up correctly, but you may well have this for other puposes.Essentially, what you do is simply connect to the SMTP port and talk to it. For this you create a TcpClient Object and a StreamWriter object and write the handshake, then the content to port 25. In spite of both the sender and receiver being included in the .eml file, you still need to provide a sender and recipient in the initial handshake.
The script here will send an email file and takes 6 parameters (including the filename). Download and use Get-Help to get a bit more information, or simply have a look at the script. There's a lot more information on scripting network connections in PowerShell in this post by Lee Holmes.
No comments:
Post a Comment