Script needed to send email notifcations using Office365

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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Technical Expert
Commented:
Here is a script that was posted on spiceworks (posting in full here so I can add changes necessary but also linked to author).
https://community.spiceworks.com/scripts/show/2372-powershell-office-365-email

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 = "MyEmail@domain.com" 
$EncryptedPW = Get-Content $EncryptedCredential | ConvertTo-SecureString 
$Credential = New-Object System.Management.Automation.PsCredential($EmailUsername, $EncryptedPW) 
$EmailFrom = "MyEmail@domain.com" 
$EmailTo = "asmith@domain.com" 
$EmailSubject = "Test Subject" 
$EmailBody = "Test Body" 
$EmailAttachments = $LogFile 
$SMTPServer = "smtp.office365.com" 
$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

Author

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

Commented:

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