Hassan Anwer
Hassan Anwer

Reputation: 367

SMTP Authentication For Office 365 With MFA Enabled

My object is to send emails using the Microsoft SMTP setting. I have configured the following things

  1. Enabling a Secure SMTP Connection for the user which I will be using
  2. Creating App Password

Test code that I am using is

import smtplib

FROM = "admin@<domain>.onmicrosoft.com"
PWD = "abcdefghi" #app password
recipient = ["[email protected]"]
TO = recipient if isinstance(recipient, list) else [recipient]
SUBJECT = "Test Message"
TEXT = "Hello"

# Function that sends email.
def send_mail(username, password, from_addr, to_addrs, msg):
    server = smtplib.SMTP('smtp.office365.com', '587')
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(username, password)
    server.sendmail(from_addr, to_addrs, message)
    server.quit()
    
# prepaire message
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    
# Send Email
send_mail(FROM,PWD,FROM,TO,message)

All of the users have MFA enabled and I want to keep it this way. If I disabled the Security Defaults setting. I am able to send the email but it increases the risk. I also check the sign-in logs and I am seeing the following error

Access has been blocked by Conditional Access policies. The access policy does not allow token issuance.

I am not able to find any option of by skipping MFA inside conditional access so that i can achieve my goal of sending email without disabling MFA for authentication apart from SMTP

Upvotes: 1

Views: 3837

Answers (1)

Rombone
Rombone

Reputation: 13

Security Defaults is a collection of Conditional Access policies, one of which actually blocks SMTP Auth. If you'd like to protect all of your users but this one mailbox, you could go for the license AAD Premium P1 (included in Business Premium) to get Conditional Access and to manually create a policy to which you can make exceptions. Remember: All users and mailboxes affected by the CA policies must be licensed correctly.

Upvotes: 0

Related Questions