Sending E-mail via Powershell script

This task can be basically performed in two ways.

First one is fairly straightforward as well as documented. The object is Net.Mail.SmtpClient, see https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

Relevant script code is similar to the next:

$mod1=New-Object Net.Mail.SmtpClient

$mod1.Host = “SMTP Gateway FQDN”

$mod1.Send(“sender@address”, “recipient@address”, “Subject line”, “Plain text body”)

Thus your script will anonymously send plain text email to the appointed SMTP gateway. Of course, if anonymous connections are allowed there.

Second method seems to be poorly documented for use with Powershell. This is an old good CDO.Message, see https://msdn.microsoft.com/en-us/library/ms872547(v=EXCHG.65).aspx

Lot of examples concerns “elder” scripting languages such as VBScript and C#, see
https://msdn.microsoft.com/en-us/library/ms875947(EXCHG.65).aspx, https://msdn.microsoft.com/en-us/library/ms527272(EXCHG.10).aspx
and others.

Portion of code relevant to the topic looks cumbersome:

$mod2=New-Object –comobject “CDO.Message”

$mod2.From=”sender@address”

$mod2.To=”recipient@address”

$mod2.Subject=”Caution”

$mod2.Htmlbody=”<b> Your mailbox may be temporarily unavailable </b>”

$namespace=”https://schemas.microsoft.com/cdo/configuration/”

$mod2.Configuration.fields.item($namespace+"sendusing")=2 # stands for sending via smtp port

$mod2.Configuration.fields.item($namespace+"smtpserver”)= “SMTP Gateway FQDN”

$mod2.Configuration.fields.item($namespace+"smtpserverport”)=25

$mod2.Configuration.fields.item($namespace+"smtpauthenticate”)=1 # stands for sending via anonymous connection

$mod2.Configuration.fields.update()

$mod2.Send()

The difference matters when you need to pass authentication or add attachment or add recipients.

For the first method, you should arrange couple of additional objects, and you may find this somewhat tiring if you are not fond of scripting. When adding additional attrubutes, you will probably have:
 

$mod1=new-object Net.Mail.SmtpClient(“SMTP Gateway FQDN”)
$email = New-Object System.Net.Mail.Mailmessage
$email.Subject = “Caution”
$email.Body = ”<b> Your mailbox may be temporarily unavailable </b>”
$email.IsBodyHTML = $true
$email.from = "sender@address ")
$email.To.Add("recipient1@address")
$email.To.Add("recipient2@address")
$email.Attachments.Add(“c:\Usersguide.doc”)
$mod.Send($email)

When adding credentials you should use:


$cred = New-Object System.Net.NetworkCredential
$cred.Domain=”Gateway1” # make sense if Gateway1 is Windows server trusting some other Windows domains
$cred.Username=”smtpuser”
$cred.Username=”P@ssw0rd”
$mod1.Credentials=$cred
$mod1.Send($email)

On the other hand, with the second method, you gain all of the above within one interface. Just need to add


$mod2.addatachment(“c:\Usersguide.doc”)

$mod2.configuration.fields($namespace+"sendusername”)=”Gateway1\smtpuser”

$mod2.configuration.fields($namespace+"sendpassword”)=”P@ssw0rd”

I prefer the second method as it allows to use basically the same sending script for different tasks or to rework it with less efforts. In addition, CDO object allows use your Exchange server as a Gateway1 mentioned above, establishing MAPI connection to Exchange. The parameter:

$mod2.Configuration.fields.item($namespace+"sendusing")=3

stands for sending the email via MAPI client. Thus you don’t need to allow anonymous connection and/or to change receive connectors.

Since Exchange 2007 you have to install version 1.2.1 of MAPI CDO library as a separate file, see KB https://support.microsoft.com/kb/171440#top and download https://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=1004 . Or you can launch your scripts on admin station where Outlook is installed.

Comments

  • Anonymous
    January 01, 2003
    Thanks...

  • Anonymous
    January 01, 2003
    Excellent.. :)

  • Anonymous
    February 03, 2016
    misspelled attachment in the lower part $mod2.addatachment(“c:Usersguide.doc”)

    missing a second T