none
Error dending via PS email out our o365 tenant RRS feed

  • Question

  • Hi,

    We have a problema qhen try to execute this script via pshell to send an email out of our o365 tenant:

    $currentMonth = Get-Date -UFormat %m

    $currentMonth = (Get-Culture).DateTimeFormat.GetMonthName($currentMonth)

    $credenciales = Import-Clixml -Path '\\localhost\c$\srvadmin\credenciales.xml'

    $fecha=$(Get-Date -UFormat "%d-%m-%Y")
    $fromaddress = "interno@loquesea.es"
    $toaddress = "externo@hotmail.com"
    $Subject = "asunto- $currentMonth"


    $body = @'
    <html> 
      <body>
      </body> 
    </html> 
    '@

    $smtpserver = "smtp.office365.com"
    $SmtpPort = '587'
    $emailSmtpUser = $credenciales.UserName
    $emailSmtpPass = $credenciales.GetNetworkCredential().password

     
    $message = new-object System.Net.Mail.MailMessage
    $message.From = $fromaddress
    $message.To.Add($toaddress) 
    $message.Subject = $Subject
    $message.body = $body
    $message.IsBodyHtml = $true
    $smtp = new-object Net.Mail.SmtpClient($smtpserver,$SmtpPort)
    $smtp.EnableSsl = $true
    $smtp.Credentials = New-Object System.Net.NetworkCredential($emailSmtpUser,$emailSmtpPass)
     
    $smtp.Send($message)

    Power Shell show these error:

    Exception calling "Send" with "1" argument(s): "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during

    MAIL FROM [LNXP265CA0072.GBRP265.PROD.OUTLOOK.COM]"

    At line:96 char:1

    + $smtp.Send($message)

    + ~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : SmtpException

    But we use credentials from this script:

    $credenciales = Get-Credential

    $credenciales.Password | ConvertFrom-SecureString

    $credenciales | Export-Clixml -Path C:\srvadmin\credenciales.xml

    Some idea of this error?

    Regards,


    Gerardo,

    Monday, November 12, 2018 10:31 AM

Answers

  • Start with this until you understand how it works.

    $body = @' 
    <html>  
      <body> 
      </body>  
    </html>  
    '@ 
    
    $mailprops = @{
        From =  'interno@loquesea.es' 
        To = 'externo@hotmail.com'
        Subject = "asunto- $([datetime]::Today.Month)" 
        body = $body
        smtpserver = 'smtp.office365.com'
        Port = 587
        UseSSL = $true
        Credential = Get-Credential
    }
    
    Send-MailMessage @mailprops
    
    You should start here instead of guessing and copying bad code: Microsoft Virtual Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/

    • Marked as answer by gerard_gorrion Monday, November 12, 2018 3:34 PM
    Monday, November 12, 2018 10:48 AM

All replies

  • Read the error message.

    "The SMTP serverrequires a secure connection or the client was not authenticated."

    You are doing a bad job of copying of obsolete code.

    Start here:

    help Send-MaolMessage -full


    \_(ツ)_/

    Monday, November 12, 2018 10:38 AM
  • Start with this until you understand how it works.

    $body = @' 
    <html>  
      <body> 
      </body>  
    </html>  
    '@ 
    
    $mailprops = @{
        From =  'interno@loquesea.es' 
        To = 'externo@hotmail.com'
        Subject = "asunto- $([datetime]::Today.Month)" 
        body = $body
        smtpserver = 'smtp.office365.com'
        Port = 587
        UseSSL = $true
        Credential = Get-Credential
    }
    
    Send-MailMessage @mailprops
    
    You should start here instead of guessing and copying bad code: Microsoft Virtual Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/

    • Marked as answer by gerard_gorrion Monday, November 12, 2018 3:34 PM
    Monday, November 12, 2018 10:48 AM
  • Start with this until you understand how it works.

    $body = @' 
    <html>  
      <body> 
      </body>  
    </html>  
    '@ 
    
    $mailprops = @{
        From =  'interno@loquesea.es' 
        To = 'externo@hotmail.com'
        Subject = "asunto- $([datetime]::Today.Month)" 
        body = $body
        smtpserver = 'smtp.office365.com'
        Port = 587
        UseSSL = $true
        Credential = Get-Credential
    }
    
    Send-MailMessage @mailprops
    You should start here instead of guessing and copying bad code: Microsoft Virtual Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/

    Hi,

    Thanks you for your response. I try to use your scritp and when insert an user credentials or Shared email credentials, the job fails with the same problem…

    some idea to program a monthly email send?

    Regards.


    Gerardo,

    Monday, November 12, 2018 11:45 AM
  • When  prompted enter your email credentials.

    SMTP won't work with O365 until you set up the O365 count to use SMTP.


    \_(ツ)_/

    Monday, November 12, 2018 11:57 AM