theme-sticky-logo-alt

Send Email from PowerShell in Office 365

6 Comments

In order to automate some tasks in Office 365 with PowerShell, sometimes you may want to send a confirmation email or a daily report by email. Luckily, there is an easy PowerShell cmdlet called “Send-MailMessage” that allows you to send an email with Exchange Server. You can also do that in Office 365, but you need to specify an important parameter: The Port number. Office 365 only accepts emails on Port 587 and secured by SSL.

Send Email from PowerShell

So the code to send an email is pretty easy:

$cred = get-credential
$body = “Just a test email”
Send-MailMessage -To vlad@demo.com -from noreply@demo.com -Subject 'test' -Body $body -BodyAsHtml -smtpserver smtp.office365.com -usessl -Credential $cred -Port 587 

If you want to learn more about PowerShell for Office 365, check out my Pluralsight course by clicking the banner below!

Send Email from PowerShell in Office 365

Follow me on Social Media and Share this article with your friends!

Leave a comment and don’t forget to like the Vlad Talks Tech Page   on Facebook and to follow me on Twitter here  for the latest news and technical articles on SharePoint.  I am also a Pluralsight author, and you can view all the courses I created on my author page.
Previous Post
Announcing the Winners of the SharePoint Fest DC
Next Post
Free Webinar: Creating Dynamic Forms in SharePoint Online Without Any Need for Technical Skills with KWizCom Forms

6 Comments

  • August 30, 2016 at 6:34 pm
    Andrew

    Is it possible to pass in a variable for the Subject the way you do for a body? It doesn’t look like it likes anything with a dollar sign in there.

    Reply
  • December 10, 2019 at 1:34 am
    Fernando

    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:3 char:1
    + Send-MailMessage -To #@####.### -from noreply@demo …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
    ion
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

    Reply
  • February 4, 2020 at 9:00 am
    Shrikant

    Following …

    Reply
  • July 28, 2020 at 11:24 am
    George Barbosa

    First run the following powershell command to make sure powershell is using TLS v1.2:

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

    Reply
  • December 22, 2020 at 12:10 pm
    Paul Gordon

    FERNANDO – I had exactly the same issue, and fought with it for several hours, before finally realising the smtp server value must be a quoted string – eg -smtpserver ‘smtp.office365.com’ as soon as I did that it all started working.

    ANDREW – Yes, it’s absolutely possible to pass variables in any of the components of the email.

    GEORGE – thanks for that, – I haven’t done that, and it all still works, but that’s a good thing to remember…

    Reply

Leave a Reply

15 49.0138 8.38624 1 0 4000 1 https://vladtalkstech.com 300 1