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 = "******@organization.com"

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

$MailMessage.IsBodyHtml = 1

$MailMessage.Subject = $MailSubject

$MailMessage.Body = $MailBody

$SmtpClient.Send($MailMessage)

Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,171 questions
Exchange Exchange Server Development
Windows for business Windows Server User experience PowerShell
{count} votes

3 answers

Sort by: Most helpful
  1. Dillon Silzer 57,826 Reputation points Volunteer Moderator
    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://learn.microsoft.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 47,901 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 ******@organization.com
    # $cred = . . . 
    
    $props = @{
        Body= " At $Event4 $Event5 was locked out on $Event3"
        BodyAsHTML = $true
    #    Credential = $cred
        From = "******@organization.com"
        Port = 587
        Subject= "User Account locked out"
        SmtpServer = "exchange.organization.com"
        To = "******@organization.com"
        UseSSL = $true
    }
    Send-MailMessage @props
    
    0 comments No comments

  3. Limitless Technology 44,746 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://learn.microsoft.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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.