Link to home
Start Free TrialLog in
Avatar of wakatashi
wakatashi

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wakatashi
wakatashi

ASKER

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

Try changing line $SMTPClient.Credentials = $mycreds

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

Open in new window

I'm afraid not - I get the same error as last time.  That's a neater way of encoding the plain text credentials though!
Does the credentials work when you send mail from outlook using the smtp server?
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