Powershell Script to send email

AMITGUY 181 Reputation points
2023-01-24T18:31:59.69+00:00

Below Powershell email script was working until we switched to office 365, can someone please advise?

$Event= Get-EventLog -LogName "Security" -InstanceID 4740 -Newest 1

$Event2 = $Event.Message | find /I "Account Name:"

$Event5 = $Event2 | Select-Object -Last 1

$Event3 = $Event.Message | find /I "Caller Computer Name:"

$Event4 = $Event.TimeGenerated

$MailBody= " At $Event4 $Event5 was locked out on $Event3"

$MailSubject= "User Account locked out"

$SmtpClient = New-Object system.net.mail.smtpClient

$SmtpClient.host = "exchange.organization.com"

$MailMessage = New-Object system.net.mail.mailmessage

$MailMessage.from = "AcctLockNotify@organization.com"

$MailMessage.To.add("user@organization.com")

$MailMessage.IsBodyHtml = 1

$MailMessage.Subject = $MailSubject

$MailMessage.Body = $MailBody

$SmtpClient.Send($MailMessage)

Microsoft Exchange Online
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,342 questions
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
525 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,446 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Dillon Silzer 54,936 Reputation points
    2023-01-24T19:14:09.21+00:00

    Hello,

    From what I can see you may be using basic authentication (username and password) which may not be allowed in your tenant.

    https://video2.skills-academy.com/en-us/exchange/clients-and-mobile-in-exchange-online/deprecation-of-basic-authentication-exchange-online

    You will need to update your script to use modern authentication:

    Modern Auth and Unattended Scripts in Exchange Online PowerShell V2

    https://o365reports.com/2020/07/04/modern-auth-and-unattended-scripts-in-exchange-online-powershell-v2/


    If this is helpful please accept answer.

    0 comments No comments

  2. Rich Matheisen 45,596 Reputation points
    2023-01-24T20:09:30.8266667+00:00

    For what reason does the code no longer work as it once did?

    There's no error checking in your code. Your code expects every connection to work, and you probably think that there's some sort of retransmission in the event of a failure (as a SMTP server would perform). But your code acts as a SMTP client, not a server. It's the responsibility of the client to either retry the connection or, in the event of exhausting the retries you're willing to tolerate, report the failure.

    Where is the SMTP server named "exchange.organization.com"? Is it using an IP address within your on-prem organization? Or is it the server in the MX record for "organization.com"?

    Your code doesn't use a SSL connection, and it's using port 25. It's also using an anonymous SMTP connection.

    This code still doesn't retry a failed connection, and it doesn't report a failure, but it uses the SMTP client port, and SSL. It can also use a credential:

    $Event= Get-EventLog -LogName "Security" -InstanceID 4740 -Newest 1
    $Event2 = $Event.Message | find /I "Account Name:"
    $Event5 = $Event2 | Select-Object -Last 1
    $Event3 = $Event.Message | find /I "Caller Computer Name:"
    $Event4 = $Event.TimeGenerated
    
    # create a credential here, using the account name that has the
    # SMTP address AcctLockNotify@organization.com
    # $cred = . . . 
    
    $props = @{
        Body= " At $Event4 $Event5 was locked out on $Event3"
        BodyAsHTML = $true
    #    Credential = $cred
        From = "AcctLockNotify@organization.com"
        Port = 587
        Subject= "User Account locked out"
        SmtpServer = "exchange.organization.com"
        To = "user@organization.com"
        UseSSL = $true
    }
    Send-MailMessage @props
    
    0 comments No comments

  3. Limitless Technology 44,091 Reputation points
    2023-01-25T16:13:00.77+00:00

    Hi,

    Thank you for posting your query.

    Kindly follow the steps provided below to resolve your issue.

    The Send-MailMessage cmdlet sends an email message from within PowerShell.

    You must specify a Simple Mail Transfer Protocol (SMTP) server or the Send-MailMessage command fails. Use the SmtpServer parameter or set the $PSEmailServer variable to a valid SMTP server. The value assigned to $PSEmailServer is the default SMTP setting for PowerShell.

    Go to this link for your reference and other troubleshooting procedures https://video2.skills-academy.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage

    Do not hesitate to message us if you need further assistance.

    If the answer is helpful kindly click "Accept as Answer" and up vote it.

    0 comments No comments