Send mail from Office365 with Powershell – 0 touch

…this post was published on 2016 March 21.

Very good Vlad’s post, how to send mail from Office365 using Powershell

$cred = get-credential
$body = Just a test email
Send-MailMessage -To -from -Subject ‘test’ -Body $body -BodyAsHtml -usessl -Credential $cred -Port 587
To change it as a <<0 touch>> script ( usefull for a scheduled task ) you should create credentials from username and clear text password.
The script in this case will be:
​$cred = New-Object System.Management.Automation.PSCredential -ArgumentList “ “, (“ ​password” | ConvertTo-SecureString -AsPlainText -Force )
$body =Just a test email
Send-MailMessage -To -from -Subject ‘test’ -Body $body -BodyAsHtml -usessl -Credential $cred -Port 587
If your credentials aren’t correct, you will receive an error message like bellow:
Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7
.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
At line:1 char:1
+ Send-MailMessage -To -from – …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage


This entry was posted in email, Office 365, Powershell. Bookmark the permalink.