none
E-mail notification using powershell script in 365

    Question

  • 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 Friday, January 24, 2014 2:02 PM
    Friday, January 24, 2014 2:01 PM

Answers

  • 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.


    ¯\_(ツ)_/¯

    • Marked as answer by bohlingj Friday, January 24, 2014 5:22 PM
    Friday, January 24, 2014 5:10 PM

All replies

  • Use Send-MailMessage.  It will negotiate the security correctly.


    ¯\_(ツ)_/¯

    Friday, January 24, 2014 2:20 PM
  • 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?
    Friday, January 24, 2014 2:25 PM
  • 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 along...


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)


    Friday, January 24, 2014 2:29 PM
  • 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

    Friday, January 24, 2014 2:35 PM
  • 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


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Friday, January 24, 2014 2:39 PM
  • 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).

    ¯\_(ツ)_/¯

    Friday, January 24, 2014 2:41 PM
  • 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.


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, January 24, 2014 2:58 PM
    Friday, January 24, 2014 2:57 PM
  • 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
    
    
    


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, January 24, 2014 3:09 PM
    Friday, January 24, 2014 3:08 PM
  • Thank you for the help, I am not very familiar with how or what splat is. How would I run this command?
    Friday, January 24, 2014 4:14 PM
  • I'm going to assume it's the use of the @ symbol to pass everything as a single string. I could be wrong though :/
    Friday, January 24, 2014 4:55 PM
  • 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


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Friday, January 24, 2014 5:05 PM
  • 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.


    ¯\_(ツ)_/¯

    • Marked as answer by bohlingj Friday, January 24, 2014 5:22 PM
    Friday, January 24, 2014 5:10 PM
  • Should "Send-MailMessage" work with PowerShell v2 and Office 365? seems not like it due to Port?
    Tuesday, May 13, 2014 1:40 AM
  • Should "Send-MailMessage" work with PowerShell v2 and Office 365? seems not like it due to Port?

    Months down the road.  OP checked as "Answered" then along comes a kibitzer ..... look it up in the dictionary.   I think they still sell them on Amazon.

    ¯\_(ツ)_/¯

    Tuesday, May 13, 2014 2:18 AM
  • In case you didn't catch it out of all the replies, the key with Office365 is to include the -UseSSL option with Send-MailMessage, as O365 requires a secure connection.
    Monday, December 4, 2017 2:22 PM