/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" MSH More replacement script update (HotFix) "," Enhanced More Function for MSH "," Get driveletters for All drives (Providers) in MSH... "," Math "," Exploring .NET types, classes and Enums from MSH "," Wipe the screen in MSH "," Blog series about Errorhandling in MSH "," This Month's line-break and Pipelines are Back "," [msh]cd.. "," MSH start. Function "," ")

Tuesday, November 15, 2005

 


Sending Mail from MSH



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.


Comments:
Anonymous Anonymous
Nice post. I tried to send an attachment but it did't work. Can you give an example with sending an attachment? Thanks
 
Blogger /\/\o\/\/
Again the some trick, the parameter is a Attachement Object the simplest overload just takes the filename

gr /\/\o\/\/

MSH>$att = new-object System.Net.Mail.Attachment("c:\test.txt")
MSH>$att


Name : test.txt
NameEncoding :
ContentDisposition : attachment
ContentStream : System.IO.FileStream
ContentId : 3dbb5f55-0b5c-4f16-b98b-61477667224b
ContentType : application/octet-stream; name=test.txt
TransferEncoding : Base64



MSH>$msg.Attachments.add($att)
 
Anonymous Alexander Kriegisch
So far, so good. I got that far myself, looking into MSDN Library, and I admit it is easy to do.

But what I could not figure out is how to actually create a custom credentials object to pass to the SMTP client. I want to create an object containing the SMTP server's user name and password for my e-mail sender.

Anybody out there who can provide a hint?
 
Anonymous Alexander Kriegisch
I am answering my own question, because I just found out by digging a bit deeper into MSDN. Here we go:

$msg = New-Object System.Net.Mail.MailMessage("sender@mydomain.com","recipient@otherdomain.org")
$msg.Subject = "Test: äöü ÄÖÜ ß"
$msg.Body = @"
This is a test message sent via Windows PowerShell.
German umlauts: ÄÖÜ äöü
End of message, see you later!
"@
$smtp = New-Object System.Net.Mail.SmtpClient("mail.mydomain.com")
$cred = New-Object System.Net.NetworkCredential("my-smtp-user", "my-smtp-password")
$smtp.Credentials = $cred
$smtp.Send($msg)
 
Post a Comment

Links to this post:

Create a Link



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?