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
100questionsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daryl BamforthTechnical 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).
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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?
0
Daryl BamforthTechnical 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.
0
Éric MoreauSenior .Net ConsultantCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.