Script needed to send email notifcations using Office365

Member_2_5306354 used Ask the Experts™
Good day.
I would like to write a script, either VBA or Powershell, which will enable me to send an email out to several recipients with an attachment if applicable, using perhaps the SMTP address for Office365.
I do not want the script to open outlook, but simply send the emails out upon triggering the script etc..

Thanks kindly for all your help
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Technical Expert
Here is a script that was posted on spiceworks (posting in full here so I can add changes necessary but also linked to author).

There are a few variables that need updating but should send quite happily from your office 365 account.

#Create Password File (Only need once) 
$Credential = Get-Credential 

$Credential.Password | ConvertFrom-SecureString | Set-Content C:\secure\creds.txt 

#Send Email 
$EncryptedCredential = "C:\secure\creds.txt" 
$EmailUsername = "" 
$EncryptedPW = Get-Content $EncryptedCredential | ConvertTo-SecureString 
$Credential = New-Object System.Management.Automation.PsCredential($EmailUsername, $EncryptedPW) 
$EmailFrom = "" 
$EmailTo = "" 
$EmailSubject = "Test Subject" 
$EmailBody = "Test Body" 
$EmailAttachments = $LogFile 
$SMTPServer = "" 
$SMTPPort = 587

$SMTPSsl = $true 

$param = @{ 
    SmtpServer = $SMTPServer 
    Port = $SMTPPort 
    UseSsl = $SMTPSsl	
    Credential = $Credential	
    From = $EmailFrom	
    To = $EmailTo	
    Subject = $EmailSubject	
    Body = $EmailBody 
    Attachments = $EmailAttachments	

Send-MailMessage @param

Open in new window


Thank you, however this is prompting me for a username and password when I run it.
I entered the password in the creds.txt file.
Any suggestions?
Daryl BamforthTechnical Expert
Enter the username and password at the prompt. This stores an encrypted key in the creds file, so you don't need to keep entering it.

Once the script has created the password file, you can keep using the rest of this script to send emails using the stored credentials.
Éric MoreauSenior .Net Consultant
Top Expert 2016


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