E-mail notification using powershell script in 365
I need a e-mail script that works using 365. Here is what I have so far and I cant get it to work.
Function EMAIL{
Param(
$emailSmtpServer = "XXXXXXXXXXX",
$emailSmtpServerPort = 587,
$emailSmtpUser = "XXXXX@XXXXX.com",
$emailSmtpPass = "Password",
$emailFrom = "XXXXX@XXXXXXX.com",
$emailTo = "XXXXXXX@XXXXXXX.com",
$emailAttachment = 'XXXXXXXXXX',
$emailSubject = "This is a test" ,
$emailBody = "How does this look?"
)
Process{
$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.Subject = $emailSubject
$emailMessage.Attachments.add($emailAttachment)
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = $emailBody
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
$SMTPClient.Send( $emailMessage )
}
}
- Edited by
bohlingj
21 hours 31 minutes ago
January 24th, 2014 9:03am
Use Send-MailMessage. It will negotiate the security correctly.
January 24th, 2014 9:23am
I could be wrong but I did some research and it said that since 365 uses TLS authentication that send-mailmessage does not work. Is this right?
January 24th, 2014 9:27am
Hi,
Are you getting error messages? 'I cant get it to work' doesn't really help us much.
Have you tried using Send-MailMessage instead (assuming you're running at least v3)?
EDIT: Aaaand I didn't hit refresh again. Nothing to see here, move al
January 24th, 2014 9:31am
I'm not getting an error message, I don't get anything back.
yes I have tried send-mailmessage I cant seem to get it to work either. Here is what I get with send-mailmessage.
PS C:\Users\XXXX> Send-MailMessage -From noreply@XXXX.com -Subject "This is a test" -To XXXX@XXXX.com -Body Testing -Port 587 -SmtpServer XXXXX
Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server
response was: 5.7.1 Client was not authenticated
At line:1 char:1
+ Send-MailMessage -From noreply@XXXX.com -Subject "This is a test" -To XXX ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
ion
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
January 24th, 2014 9:38am
This is what I use for sending email via gmail, try playing with these parameters:
$param = @{
SmtpServer = 'smtp.gmail.com'
Port = 587
UseSsl = $true
Credential = 'email.address@gmail.com'
From = 'email.address@gmail.com'
To = 'someuser@domain.com', 'someotheruser@domain.com'
Subject = 'Subject Line'
Body = 'Email body'
Attachments = 'C:\attachment.csv'
}
Send-MailMessage @param
January 24th, 2014 9:41am
I could be wrong but I did some research and it said that since 365 uses TLS authentication that send-mailmessage does not work. Is this right?
TLS is the newer version of SSL. use the -UseSSL switch. Port 587 is set to discover the login security and will accept either SSL or non-SSL connections. YOU can only authenticate via SSL (TLS).
January 24th, 2014 9:43am
Here is the method I use. I just retested to my own account and it works correctly"
$mailprops=@'
UseSsl=$true
Port=587
SmtpServer'smtp.office365.com'
From='userid@domain.com'
Credential='userid@domain.com'
'@
Send-MailMessage -to jvierra@msn.com -subject test -body test @mailprops
I use a splat because it can be stored in my profile and reused very quickly. The SMTP server is the same fo all Office365 accounts when authenticating to the smtp relay service.
January 24th, 2014 9:59am
I found that some strings fail with splatting on send mailmessage. Tis always works
$userid='userid@domain.com'
Send-MailMessage `
-To 'someuser@msn.com' `
-subject 'test' `
-body 'test' `
-UseSsl `
-Port 587 `
-SmtpServer 'smtp.office365.com' `
-From $userid `
-Credential $userid
January 24th, 2014 10:10am
Thank you for the help, I am not very familiar with how or what splat is. How would I run this command?
January 24th, 2014 11:16am
I'm going to assume it's the use of the @ symbol to pass everything as a single string. I could be wrong though :/
January 24th, 2014 11:58am
I'm going to assume it's the use of the @ symbol to pass everything as a single string. I could be wrong though :/
All you have to do is ask the intarwebs and they will tell you:
http://ss64.com/ps/syntax-hash-tables.html
January 24th, 2014 12:07pm
Please note that splatting on Send-MailMessage in V4 no longer works. It works in V2 and V3.
We require V3 to use -Port. InV4 use full parameters.I recommend using this until you get all of your values set correctly.
$userid='yourid@yourdoamin.com'
$creds=Get-Credential $userid
Send-MailMessage `
-To 'someone@soemwhere.com' `
-Subject 'Test' `
-Body 'Test' `
-UseSsl `
-Port 587 `
-SmtpServer 'smtp.office365.com' `
-From $userid `
-Credential $creds
You must use
'smtp.office365.com' to connect to smtp on Office 365.
January 24th, 2014 12:12pm