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
https://absolute-sharepoint.com/2016/03/send-email-from-powershell-in-office-365.html#comment-378739

​​
$cred = get-credential
$body = Just a test email
Send-MailMessage -To destination@mailserver.com -from noreply@mailserver.com -Subject ‘test’ -Body $body -BodyAsHtml -smtpserversmtp.office365.com -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 “noreply@mailserver.com “, (“noreply@mailserver.com ​password” | ConvertTo-SecureString -AsPlainText -Force )
$body =Just a test email
Send-MailMessage -To destination@mailserver.com -from noreply@mailserver.com -Subject ‘test’ -Body $body -BodyAsHtml -smtpserversmtp.office365.com -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 destination@mailserver.com -from noreply@mailserver.com – …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + 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.