Modify script to use Office365 and port 587

Tek Info
Tek Info used Ask the Experts™
on
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###
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
You need to pass a credential object, and you need to tell Send-MailMessage to use SSL and the port:
###PROGRAM START###
$username = "username@yourdomain.com"
$password = "TopSecret"
# 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.office365.com'
		Subject =		$subject
		Body =			$MessageBody
		BodyAsHtml =	$true
		Attachment =	(Join-Path $PSScriptRoot 'abc.pdf')
		UseSsl =		$true
		Port =			587
		Credential =	New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, ($password | ConvertTo-SecureString -AsPlainText -Force)
	}
	Send-MailMessage @splat
###PROGRAM END###

Open in new window

Author

Commented:
Can the credential be embedded in the script?
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

@Tek Info,

oBdA's script has the credential embedded.
Really it is the only difference from mine..
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
True dat.
@oBdA,

Touche, I did miss one item however :)

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

Author

Commented:
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.

Author

Commented:
This was changed already to a valid email, and it still does not work.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial