Powershell 2.0 - send e-mail via Office 365

I was expecting this to be easy, but it seems that there are many ways to skin this particular cat, and some of them require Powershell version 3 or 4.

I'm running Powershell 2 under Windows Server 2008 R2.  I'd like to use Powershell to send an e-mail message via Office 365's SMTP server.  This requires SSL on port 587, and authenticated SMTP.  I know that the SMTP server, port, and credentials I'm using are correct.

This is what I have so far:

$From = "user@domain.com"
$To = "recipient@anotherdomain.com"
$SMTPServer = "smtp.office365.com"
$SMTPPort = "587"
$Username = "user@domain.com"
$Password = "plaintextpassword"
$subject = "This is the subject line"
$body = "This is the message body"

$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($Username, $secpasswd)

send-mailmessage -from $From -to $To -subject $subject -body $body -smtpserver $SMTPServer -credential $mycreds -UseSSL

Open in new window


...but it fails with the following error:

Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
At C:\test\mailtest.ps1:15 char:17
+ send-mailmessage <<<<  -from $From -to $To -subject $subject -body $body -smtpserver $SMTPServer -credential $mycreds -UseSSL
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
   ion
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

Open in new window


How can I make this work in Powershell v2.0, please?
LVL 1
wakatashiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
There is no port parameter with Send-MailMessage in PS 2.0.. use the following code instead of Send-MailMessage..
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer,$SMTPPort) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = $mycreds
$SMTPClient.Send($From, $To, $subject, $Body)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wakatashiAuthor Commented:
Hi Subsun - thanks for super-quick response!

When I replace the send-mailmessage line with the code above, I get the following:

Exception calling "Send" with "4" argument(s): "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated"
At C:\backup\test3.ps1:17 char:17
+ $SMTPClient.Send <<<< ($From, $To, $subject, $Body)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Open in new window

0
SubsunCommented:
Try changing line $SMTPClient.Credentials = $mycreds

to
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)

Open in new window

0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

wakatashiAuthor Commented:
I'm afraid not - I get the same error as last time.  That's a neater way of encoding the plain text credentials though!
0
SubsunCommented:
Does the credentials work when you send mail from outlook using the smtp server?
0
wakatashiAuthor Commented:
I've been trying on on and off for about 3 months to get this to work, and the credentials were fine when I started on this!  But since then it seems that BT (who provide the Office 365 mailbox) have disabled the mailbox, and so the credentials were being rejected.  I've re-enabled the mailbox, and now the script, with your modifications, works fine.

Thank you very much for your help, Subsun!  Working script is:

$From = "user@domain.com"
$To = "recipient@anotherdomain.com"
$SMTPServer = "smtp.office365.com"
$SMTPPort = "587"
$Username = "user@domain.com"
$Password = "plaintextpassword"
$subject = "This is the subject line"
$body = "This is the message body"


$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer,$SMTPPort) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
$SMTPClient.Send($From, $To, $subject, $Body)

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.