[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3844
  • Last Modified:

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?
0
wakatashi
Asked:
wakatashi
  • 3
  • 3
1 Solution
 
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
 
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
 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

 
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

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now