Link to home
Start Free TrialLog in
Avatar of Tek Info
Tek Info

asked on

Modify script to use Office365 and port 587

I would like to change this script so that uses the smtp server:
Port 587
Encryption TLS
Password: xxxx

      # Invokes the Send-MailMessage function to send notification email
      $splat = @{
            From =                  ''
            To =                  Get-Content -Path ( Join-Path $PSScriptRoot 'DistributionList.txt')
            SmtpServer =      ''
            Subject =            $subject
            body =                  $MessageBody
            BodyAsHtml =      $true
            Attachment =      ( Join-Path $PSScriptRoot 'abc.pdf' )
      Send-MailMessage @splat
Avatar of ITguy565
Flag of United States of America image

This should do it :

# Invokes the Send-MailMessage function to send notification email
$splat = @{
    From       = ''
    To         = Get-Content -Path ( Join-Path $PSScriptRoot 'DistributionList.txt')
    SmtpServer = ''
    Subject    = $subject
    body       = $MessageBody
    BodyAsHtml = $true
    Attachment = ( Join-Path $PSScriptRoot 'abc.pdf' )
    Port       = "587"
Send-MailMessage @splat -usessl -Credential $(Get-Credential)

Open in new window

Avatar of oBdA

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


Can the credential be embedded in the script?
@Tek Info,

oBdA's script has the credential embedded.
Really it is the only difference from mine..
Not quite, -UseSsl is missing in yours as well ;)
Send-MailMessage @splat -usessl -Credential $(Get-Credential)

I added it to the command string rather than to the @splat
True dat.

Touche, I did miss one item however :)

Didn't change the smtpserver  **Face to Palm**
SmtpServer =      ''
Thanks @oBdA however the script produces an error message.

Send-MailMessage :  Transaction Failed.  The server response was:  5.2.0
STOREDRV.Submission.Exception: SendAsDeniedException.  MapiExceptionSendAsDenied:  Failed to process message due to a
permanent exception with message.  Cannot sumit message.....

+CategoryInfo             :InvalidOperation:  (System.Net.Mail. SmtpClient:  StmtpClient) [Send-MailMessage], SmtpException
+FullyQualifiedErrorId: SmtpException, Microsfoft.Powershell.Commands.Send Message
From =                  ''

to a valid e-mail address associated with the account you are attempting to authenticate through...

I would be willing to bet is on a domain you do not own.
This was changed already to a valid email, and it still does not work.
Is the From address the same as the one you used in the credential object? If not, the account used for the credentials must be allowed to send as the From user.