Cannot use scripts or IOT devices to send emails via my outlook account

Brett Flagg 1 Reputation point
2022-09-01T12:05:58.647+00:00

Outlook.com account.
Scripts and a printer for example.
I would get updates on my backups delivered to my outlook.com account and with the printer, scan to email. I've noticed it in the past but didn't have time.
Now that I kinda need it working again, i cannot get it to work.

Using the :
smtp-mail.outlook.com, 587, w/ StartTLS returns authentication failure. (i've tried office.outlook.com as well)
I know my user/name and password is correct, because, I get email works thru multiple devices, and I can log into outlook.com with the same creds from different machines.
Is MS blocking this stuff now?

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,657 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,435 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Rich Matheisen 45,431 Reputation points
    2022-09-01T14:59:03+00:00

    The only thing that changed (some time ago) is the Microsoft stopped allowing really old versions of TLS to be used (e.g., TLS 1.1). If you're using PowerShell (and Send-MailMessage), add this to the beginning of your code:

    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12  
    

    That's just a guess, though. You haven't specified the reason the connection was rejected.

    0 comments No comments

  2. Brett Flagg 1 Reputation point
    2022-09-01T21:30:51.69+00:00

    Thanks for the time and looking at this.

    The error I'm getting:
    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 Client not authenticated to send mail. Error: 535 5.7.139
    Authentication unsuccessful, the user credentials were incorrect. [CH2PR10CA0024.namprd10.prod.outlook.com]"
    At C:\Batch\SendLog.ps1:41 char:1

    • $SMTPClient.Send($SMTPMessage)

    The thing is the creds are correct!

    Here's the code that's in use now..(it's called after a robocopy is complete and is designed to send the logs to me)

    $User = "someuser@harsh.com .com"
    $File = "c:\batch\MyPassword.txt" (the MyPassword.txt was updated to be sure)
    $dirPath ="C:\Logs"
    $sendThisFile = ""

    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = "someuser@harsh.com .com"
    $EmailFrom = "someuser@harsh.com .com"
    $SMTPServer = "smtp.outlook.com"

    $ChkFile = $dirPath + "\dbackup.txt"
    $fileExist = Test-Path $ChkFile

    if ($fileExist -eq $True) {
    $sendThisFile = $ChkFile;
    $Subject = "Daily Backup Notification" ;
    $Body = "Here are last night backup results. Review attachment." }
    elseif
    (
    $sendThisFile = $dirPath + "\wbackup.txt"
    )
    {
    $Subject = "Weekly Backup Notification";
    $Body = "Here is the Weekly Full Backup Notification. Review Attachment."
    }
    else
    {
    $sendThisFile = $dirPath + "\error.txt";
    $Subject = "Error on Backup"
    $Body = "An error has occurred with last nights backup. Backup drive was not available. Review attachment for details."
    }

    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 // added as a test

    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $attachment = New-Object System.Net.Mail.Attachment($sendThisFile)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)

    Exit

    But it's also a few other devices..like my epson workforce scanner,, scan to email function. used to work.. now.. nope.,

    0 comments No comments

  3. Brett Flagg 1 Reputation point
    2022-09-01T21:54:29.717+00:00

    Update.. broke it down to the basics...and it fails with STARTTLS (i'm running v5.x of PS). SO it is something on the MS side. They just haven't shared the info correctly...

    $To = "user@harsh.com .com";
    $Sender = "user@harsh.com .com";
    $Subject = "Test of PS send mail";
    $Body = "Hi, this is a test.";
    $SMTPServer = "smtp-mail.outlook.com";

    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls12

    $SMTPMessage = New-Object System.Net.Mail.MailMessage($Sender,$To,$Subject,$Body);
    $SMTPClient.EnableSsl = $true;
    $SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587);
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage);

    ERROR:
    PS C:\Batch> C:\Batch\Test.ps1
    Exception calling "Send" with "1" argument(s): "Error in processing. The server response was: 5.7.3 STARTTLS is
    required to send mail [CH2PR04CA0030.namprd04.prod.outlook.com]"

    0 comments No comments

  4. Rich Matheisen 45,431 Reputation points
    2022-09-02T01:57:47.34+00:00

    In your 1st example you got the error "Authentication unsuccessful, the user credentials were incorrect" (the server response was "5.7.57 Client not authenticated to send mail. Error: 535 5.7.139").

    In your 2nd example the server responded with "The server response was: 5.7.3 STARTTLS is required to send mail"

    In your 1st example you used [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 // added as a test

    But in your 2nd example you used [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls12

    The 1st example must have received a positive (220 status) message in response to the STARTTLS command because the "AUTH LOGIN" command is sent after the STARTTLS.

    The 2nd example received a 5xx status message in response to the "STARTTLS" and never made it to the "AUTH LOGIN" command.

    What I don't know is what the error was before you modified the code! If it was the STARTTLS that was failing, then my previous was correct and your setting of the security protocol using "-bor" caused the problem in your 2nd example.

    On the other hand, if it was the credential failure then my suggestion to explicitly use TLS12 was unnecessary (although it doesn't cause a problem).

    You used to be able to get the SMTP protocol trace from the M365 Admin Center (it used to be under the "mail flow" => "message trace" -- if the message came from outside the M365 system). If it's there (they may have moved it!) you should be able to decode the user name and password (they're just UUENCODED) and verify that they're correct.

    0 comments No comments

  5. Limitless Technology 39,471 Reputation points
    2022-09-09T08:06:10.32+00:00

    Hi Brett,

    Thank you for posting your query.

    To resolve your query kindly do the steps below.

    Go to this page https://account.live.com/names/Manage.

    Under Account Aliases, click Add Email.

    Under Add an alias, click the radio button for Add an existing email address as a Microsoft account alias.

    Click the button Add Alias.

    Go to this link for your reference https://support.microsoft.com/en-us/office/unable-to-send-emails-from-outlook-com-connected-accounts-from-outlook-app-6220dbfa-73f3-45a9-9947-e16b9ed441b0

    If issue persist try to start Outlook in safe mode, click the Windows key. In the search box, type outlook.exe /safe and press Enter.

    Check this link for additional troubleshooting procedures to resolve your query https://support.microsoft.com/en-us/office/i-can-t-send-or-receive-messages-in-outlook-97748418-bbd5-4743-a05b-581f22a466dd

    ---------------------------------------------------------------------------------------------------------------------------

    If the answer is helpful kindly click "Accept as Answer" and upvote it. Thanks.