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:  smtp.office365.com
Port 587
Encryption TLS
Username:  username@yourdomain.com
Password: xxxx



###PROGRAM START###
      # Invokes the Send-MailMessage function to send notification email
      $splat = @{
            From =                  'info@info.com'
            To =                  Get-Content -Path ( Join-Path $PSScriptRoot 'DistributionList.txt')
            SmtpServer =      'smtp.server.com'
            Subject =            $subject
            body =                  $MessageBody
            BodyAsHtml =      $true
            Attachment =      ( Join-Path $PSScriptRoot 'abc.pdf' )
      }
      Send-MailMessage @splat
###PROGRAM END###
Encryption* SendmailScripting Languages

Avatar of undefined
Last Comment
oBdA

8/22/2022 - Mon
ITguy565

This should do it :

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

Open in new window

ASKER CERTIFIED SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Tek Info

ASKER
Can the credential be embedded in the script?
ITguy565

@Tek Info,

oBdA's script has the credential embedded.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ITguy565

Really it is the only difference from mine..
oBdA

Not quite, -UseSsl is missing in yours as well ;)
ITguy565

Send-MailMessage @splat -usessl -Credential $(Get-Credential)
###PROGRAM END###

I added it to the command string rather than to the @splat
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
oBdA

True dat.
ITguy565

@oBdA,

Touche, I did miss one item however :)

Didn't change the smtpserver  **Face to Palm**
SmtpServer =      'smtp.office365.com'
Tek Info

ASKER
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ITguy565

Change
From =                  'info@info.com'

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

I would be willing to bet info@info.com is on a domain you do not own.
Tek Info

ASKER
This was changed already to a valid email, and it still does not work.
oBdA

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.