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:  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###
Avatar of ITguy565
ITguy565
Flag of United States of America image

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
Avatar of oBdA
oBdA

Link to home
membership
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

ASKER

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)
###PROGRAM END###

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

Touche, I did miss one item however :)

Didn't change the smtpserver  **Face to Palm**
SmtpServer =      'smtp.office365.com'
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
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.
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.