emailsmtpexchange-serversmtp-auth

How to connect to Microsoft Exchange Smtp?


Usually, for Gmail and outlook we have app passwords to connect to SMTP (though we can use email passwords by enabling less secure app access).

But for my Microsoft Exchange mail I can't find the app password, nor I can connect my app with the mail to send emails.

import smtplib
password = "mypassword"
email = "usrname@tsac-uae.com"
s = smtplib.SMTP('smtp.office365.com', 587)
s.starttls()
s.login(email, password)

I get an error saying

smtplib.SMTPAuthenticationError: (535, b'5.7.139 Authentication unsuccessful, 
SmtpClientAuthentication is disabled for the Tenant. 
Visit https://aka.ms/smtp_auth_disabled for more information. 
[AM9P192CA0015.EURP192.PROD.OUTLOOK.COM]')

To avoid these complications, I need to use an app password. But I can't find how to set up app password in exchange. Also, the steps said in the website is outdated. I can't find enable SMTP anywhere.


Solution

  • You need to enable SMTP submission (an organization-wide setting) within your Microsoft Tenant as per the link you get in the error message, which expands to https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission.

    The change can only be performed via powershell. An easy way to perform this change is by logging in at https://shell.azure.com. To use Exchange Online cmdlets in the Azure Cloud Shell, the Exchange Online cmdlets need to be imported using Connect-EXOPSSession. Once connected, to enable SMTP submission use

    Set-TransportConfig -SmtpClientAuthenticationDisabled $false 
    

    Make sure you read the article linked above to understand the security implications. Also beware that

    If your authentication policy disables basic authentication for SMTP, clients cannot use the SMTP AUTH protocol even if you enable the settings outlined in this article. For more information, see Disable Basic authentication in Exchange Online.