This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
Sending mail using SMTP in MSH is very easy.
First we Need a SMTPClient Object :
$SmtpClient = new-object system.net.mail.smtpClient
Set the SMTP-server :
$SmtpClient.host = "smtp.foo.com"
And Send a Mail :
$SMTPClient.Send("no@spam.mow","SomeOne@foo.Com","Greetings from MSH","this Greeting is Send by MSH")
The Parameters are From - To - Title - Body.
as this is not very handy we could make a script like this :
$SmtpServer = "smtp.Server.com"
$From = "no@spam.mow"
$To = "SomeOne@Foo.com"
$Title = "Greetings from MSH"
$Body = "Hello, SomeOne `n Greetings from MSH"
$SmtpClient = new-object system.net.mail.smtpClient
$SmtpClient.host = $SmtpServer
$SmtpClient.Send($from,$to,$title,$Body)
But What if we have more to Configure, like Port,Credentials and SSL ?
As you can see below you can set them all on the $SMTPclient Object
MSH>$SmtpClient
Host : smtp.Server.com
Port : 25
UseDefaultCredentials : False
Credentials :
Timeout : 100000
ServicePoint : System.Net.ServicePoint
DeliveryMethod : Network
PickupDirectoryLocation :
EnableSsl : False
ClientCertificates : {}
So we are all done now ... But then Again ... maybe not ......(C)LSL
what if we want to set a CC or add an Attachement ?, as there are no parameters for that
Let's look again at that SEND Method :
MSH>$SMTPClient.Send.OverloadDefinitions
System.Void Send(String from, String recipients, String subject, String body)
System.Void Send(MailMessage message)
It has also a Second Overload that takes a MailMessage as Input, as we have seen before in
Exploring .NET types, classes and Enums from MSH this is an .NET-Class that represents a Object, on with you can fill the Properties, and then give it as argument to the Method.
I little problem here is that the overload definition only gives you the ClassName not the Path and you need that to create the Object in MSH.
a little trick for this, Just give it something and the Errormessage will tell you the compleet Path (the Number of arguments most be right for this to work)
So :
MSH>$SMTPClient.Send("Foo")
Cannot convert argument "0", with value: "Foo", for "Send" to type "System.Net.Mail.MailMessage".
At line:1 char:17
+ $SMTPClient.Send( <<<< "Foo")
... And now we know the Full Path and can Create the Object in MSH :-)
MSH>$Msg = new-object system.net.mail.MailMessage
MSH>$msg
From :
ReplyTo :
To : {}
Bcc : {}
CC : {}
Subject :
SubjectEncoding :
Headers : {mime-version}
Body :
BodyEncoding :
IsBodyHtml : False
Attachments : {}
AlternateViews : {}
As you can see you are a lot more flexible in creating your MailMessage now !
So just configure the $msg Object with the properties you want, and then send it like this :
$SmtpClient.Send($msg)
This may look a bit strange at first (if you come to MSH without OOP or a .NET background that is.) but as we also have seen here :
Lets Query WMI from MSH this way of working is very powerfull and easy when you get used to it and it's very readable in you code
(try to make the script as show for the "Simple" way to send mail (without the MailMessage Object) again but now with the $MSG object, you will see how nice the code will look.without the need to dim all that helper-variables !
also if you want to look, how the message is constructed till now or what properties there are to fill, you just type $MSH and hit the enter-key.
so you see mailing from MSH is very easy and powerfull, I'm sure you will find a good use for it ;-)
Enjoy.
gr /\/\o\/\/
P.S. and again this works very nice with the ObjectViewer (
MSH Object Viewer ), try:
OV($MSG)
, than you can just configure most of the properies from the GUI and go on sending the Mail.