Thread: Sending eMails
View Single Post
Old 10-20-2006   #6 (permalink)
mabster


 
 

Re: Sending eMails

No worries, Fred.

The first step is creating an instance of SmptClient, and passing in a
mail server name to its constructor. In C# that'd look like this:

System.Net.Mail.SmtpClient sc = new
System.Net.Mail.SmtpClient("mailserver.example.com")

.... but in PS it's:

new-object Net.Mail.SmtpClient -arg "mailserver.example.com"

(The -arg parameter of new-object is how we pass stuff into an object's
constructor.)

So once we have our object, we just want to call its Send method. In C#
we'd do this:

sc.Send("from@powershell.com", "to@example.com", "Subject", "Here is
some mail from PowerShell.");

.... but in PS we don't even need a variable - we can just use the
instance inline by wrapping it in parentheses, like this:

(new-object Net.Mail.SmtpClient -arg
"mailserver.example.com").Send("from@powershell.com", "to@example.com",
"Subject", "Here is some mail from PowerShell.")

Note that I could have done it on two lines with a variable:

$sc = new-object Net.Mail.SmtpClient -arg "mailserver.example.com"
$sc.Send("from@powershell.com", "to@example.com", "Subject", "Here is
some mail from PowerShell.");

Does that help?

Fred J. wrote:
> Matt,
> I found the .NET Framework Class Library references in the MSDN library
> for SmtpClient Class and SmtpClient.Send Method (MailMessage) .
> However, can you give me some hints on how you transform that
> documentation into the example you showed? Or is there another class
> that i missed?
>
> Thank you,
> Fred Jacobowitz
>
> Matt Hamilton wrote:
>> That seems a bit excessive. How about:
>>
>> (new-object Net.Mail.SmtpClient -arg
>> "mailserver.example.com").Send("from@powershell.com", "to@example.com",
>> "Subject", "Here is some mail from PowerShell.")

>

My System SpecsSystem Spec