Script needed to send email notifcations using Office365

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
Who is Participating?
Daryl BamforthConnect With a Mentor Technical ExpertCommented:
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

100questionsAuthor 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 BamforthConnect With a Mentor Technical ExpertCommented:
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 ConsultantCommented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.