Use PowerShell
Real Admins Script
Real Admins Script
In my Exploring the .NET Framework series, I introduced the System.Net.Mail.MailMessage class. Being able to create a MailMessage object is all well and good, but if you can’t send it, it’s really not helpful.
To send an email from PowerShell using the .NET Framework, you can use the System.Net.Mail.SMTPClient class.
First, you need to create your MailMessage
PS C:\scripts\PowerShell> $message = New-Object System.Net.Mail.MailMessage –ArgumentList steve@usepowershell.com, nobody@adomain.com, ‘This is a test’, ‘I’m going to send an email via PowerShell’
If you need to add an attachment, you can add one with the System.Net.Mail.Attachment class.
To create the attachment, we’ll create an use an overload of the Attachment class that allows us to specify a filename and a MIME type (the MIME types are available in several enumerations: System.Net.Mime.MediaTypeNames.Application, System.Net.Mime.MediaTypeNames.Image, and System.Net.Mime.MediaTypeNames.Text – I’ve had a bit of trouble accessing those enumerations via PowerShell, but you can specify the name of the enumeration value in a string).
PS C:\scripts\PowerShell> $attachment = New-Object System.Net.Mail.Attachment –ArgumentList ‘c:\docs\test.xls’, ‘Application/Octet’
PS C:\scripts\PowerShell> $message.Attachments.Add($attachment)
After we have our email and optional attachment, we’ll create the SMTP client and have it configured to use the SMTP server of our choice.
The constructor of the SMTPClient class that we will use is
SmtpClient(
String host,
)
PS C:\scripts\PowerShell> $smtp = New-Object System.Net.Mail.SMTPClient –ArgumentList 10.10.10.15
All that is left is sending our email..
PS C:\scripts\PowerShell> $smtp.Send($message)
Have fun!
NOTE:
There is a shortcut to creating the MailMessage object like we did.. there is an overload of Send where you can pass in four strings (from, to, subject, and message). The SMTPClient handles the creation of the MailMessage in the background.
A faster method would be
PS C:\scripts\PowerShell> $smtp = New-Object System.Net.Mail.SMTPClient –ArgumentList 10.10.10.15
PS C:\scripts\PowerShell> $smtp.Send(‘steve@usepowershell.com’, ‘nobody@adomain.com’, ‘This is a test’, ‘I’m going to send an email via PowerShell’)
March 4, 2009 - 12:59 pm
Thanksfully in CTP3 we have Send-MailMessage!
June 23, 2009 - 12:58 pm
Way too much work. So there is no Linux equivalent mail command in Powershell? In Linux you could just
get-whatever “stuff” | mail -s “This is subject” user@exapmle.com
June 23, 2009 - 2:57 pm
As the previous comment mentions, there is a Send-MailMessage in Version 2 (this is the first release of PowerShell) which provides similar functionality. Also, you don’t have to go through those steps every time, you could wrap that in a script or function (which is one of the points of a shell language anyway.. solve the problem once and reuse the functionality).
September 24, 2009 - 1:47 pm
I think the poster’s point is that while yes WE can write our out simple mail send function, in other OS’s that has been handled already,and the users’ don’t have to re-invent the wheel over and over.
Sending email is a such basic function, a simple and quick method of sending it SHOULD be part of powershell.
September 24, 2009 - 2:21 pm
Gregory,
Thanks for chiming in.
I agree that it is a simple function and should be part of the shell and it is in V2.
And just to be nitpicky, I don’t believe that mail in Linux is an “OS” command, but part of the shell installed.
Also, mail didn’t first surface as a shell command until about V6 of AT&T’s Unix (per the mail man page), so V2 isn’t that bad.
July 6, 2010 - 9:39 am
You can choose not to believe it, but the “mail” command has nothing to do with the shell, it’s a program usually installed on distribution which allow you to send mails as well as reading your local mail-box with a textual interface.
For example, installing the “mailutils” package on Debian install the “mail” program.
September 18, 2009 - 1:53 pm
How about to send/attache multiple files, like *.txt?
September 24, 2009 - 2:30 pm
To add something like *.txt, I would probably do:
PS C:\scripts\PowerShell>Dir *.txt | foreach-object { $attachment = New-Object System.Net.Mail.Attachment –ArgumentList $_.FullName, ‘text/plain’; $message.Attachments.Add($attachment) }
#not tested.. just off the top of my head..
October 5, 2009 - 5:01 pm
I’ll try it out. Thx for the response.
October 5, 2009 - 5:53 pm
It works, but only attaches the last file in the dir list.
December 7, 2009 - 5:30 pm
About the Net.Mime.MediaTypeNames classes in powershell.
I’ve found an article that gives the solution how to get it from powershell.
The problem here is the “.” between MediaTypeName. You have to replace it by a “+”.
This gives for a jpeg:
[Net.Mime.MediaTypeNames+Image]::Jpeg
This gives you just a string back:
“image\jpeg”