The following is an example of how to send an email using PowerShell via O365. If you need to send via Gmail or another provider, it should just be a case of swapping out the SMTP settings.
The email account defined with SMTPClient.Credentials will need a O365 license and will need to have permission to send from the address defined by $EmailFrom. If $EmailFrom is a different mailbox
# O365 Email setup $SMTPServer = "smtp.office365.com" $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) $SMTPClient.EnableSsl = $true # Change this sections variables as required $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("O365User@domain.com", "Password"); $EmailFrom = "O365User@domain.com" $EmailTo = "Recipient@domain.com" $EmailSubject = "Just a Test Mail" $EmailBody = "<strong>A Test HTML Email</strong>" # Send the Email $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$EmailSubject,$EmailBody) $SMTPMessage.IsBodyHTML = $true # Set to $false or delete this line if you want plain text $SMTPClient.Send($SMTPMessage)
Recent Comments