Enable SMTP Authentication in Office 365 with PowerShell

Hey there, email enthusiasts! Ready to unravel the secrets of seamless communication in Office 365?

In this blog, we’re diving deep into the world of SMTP authentication, empowering you with PowerShell.

Buckle up for a journey that transforms your email game! 🚀

Overview of SMTP Authentication in Office 365

What is SMTP Authentication and Why is it Used?

SMTP authentication is a security mechanism that allows email clients and other applications to authenticate with an SMTP server before sending emails through it.

When enabled in Office 365, SMTP authentication requires clients to provide valid credentials – like a username and password – when connecting to Office 365’s SMTP servers to send outgoing emails. This prevents unauthorized use of the SMTP server for sending spam or malicious emails.

Some key reasons why organizations use SMTP authentication in Office 365 include:

  • Security – Requiring authentication prevents spammers and hackers from routing emails through your Office 365 tenants. This protects your organization’s email infrastructure.
  • Accountability – With SMTP authentication, every sent email can be traced back to a specific authorized user account. This improves accountability.
  • Control – Admins can selectively enable or disable SMTP authentication for specific users or groups. This provides control over who can route emails through Office 365.
  • Auditing – SMTP authentication logs can be used for monitoring and auditing who is sending outbound emails.
  • Compliance – Many security policies and regulatory compliance standards require SMTP authentication to be implemented.

Overall, SMTP authentication acts as an extra layer of security for your email environment when properly configured in Office 365.

How SMTP Authentication Works in Office 365

Here is a quick overview of how SMTP authentication works in Office 365:

  1. A user or application attempts to connect to Office 365’s SMTP servers on ports 25, 587 or 465 to send an email.
  2. The Office 365 SMTP server requires the client to authenticate using a username and password.
  3. The client provides valid credentials that are checked against Office 365’s directory.
  4. If the credentials are valid, the SMTP server allows the connection and email to be sent.
  5. If authentication fails, Office 365 blocks the connection and disallows use of the SMTP server.

By default, SMTP authentication is enabled in Office 365 for most organizations. However, it can be selectively disabled for certain users or groups by admins.

Office 365 supports common SMTP authentication mechanisms like LOGIN, PLAIN and XOAUTH2. Admins can view and audit authentication logs in the Office 365 Security & Compliance center.

Pros and Cons of Enabling SMTP Authentication

Here are some key benefits of enabling SMTP authentication in Office 365:

Pros:

  • Improved email security and prevention of spam
  • Accountability for sent emails back to user accounts
  • Control over who can send outbound emails
  • Auditing of SMTP connections and email sending
  • Adherence to security policies and compliance standards

However, there are also some downsides to consider:

Cons:

  • Added complexity when configuring devices and apps
  • Need to manage credentials for shared mailboxes and services
  • Compatibility issues with legacy on-premise systems
  • Additional administrative overhead to manage and audit
  • Potential disruption if SMTP authentication is misconfigured

Overall, for most organizations, the security pros of implementing SMTP authentication outweigh the cons. But the cons should be evaluated especially when enabling SMTP auth broadly across an organization.

Checking If SMTP Authentication is Enabled in Office 365

Before making any configuration changes, it’s important to verify whether SMTP authentication is already enabled in your Office 365 tenant. There are two main ways to check:

Verifying SMTP Authentication Status in the Admin Portal

As an Office 365 admin, you can easily check the status of SMTP authentication directly in the Office 365 admin center:

  1. Login to the Office 365 admin center at admin.microsoft.com.
  2. Navigate to Settings > Org settings in the left menu.
  3. Scroll down to the Services section and find the card for Modern Authentication.
  4. In the Modern Authentication section, look for the Authenticated SMTP setting.
  5. If the toggle is enabled and blue, SMTP authentication is turned on for your Office 365 organization.
  6. If the toggle is disabled and gray, SMTP authentication is currently turned off.

This allows you to quickly see if SMTP authentication is enabled at the organization level within Office 365.

Some key things to note:

  • Even if enabled for the organization, SMTP authentication can still be selectively disabled for specific users or groups. Always check.
  • Microsoft recently enabled SMTP authentication by default for most Office 365 tenants. But organizations created before 2020 may still have it disabled.
  • In some cases, like for tenants using Microsoft Defender for Office 365, SMTP authentication is managed through separate policy settings.

So while the admin portal makes it easy to check status at the organization level, further testing is required to confirm SMTP authentication behavior for sending email.

Testing SMTP Authentication with Telnet or SMTP Clients

To validate how Office 365 is actually handling SMTP connections from clients, you need to directly test SMTP authentication using a tool like Telnet or an SMTP client library.

Here is how to test whether SMTP authentication is working from the command line:

  1. Connect to Office 365’s SMTP server at smtp.office365.com on port 587 using Telnet or OpenSSL. $ telnet smtp.office365.com 587
  2. Try sending an email from an Office 365 accepted domain without any authentication. For example: EHLO contoso.com MAIL FROM: [email protected] RCPT TO: [email protected] DATA From: [email protected] To: [email protected] Testing SMTP authentication . QUIT
  3. If the command fails with an error like “Authentication Required” or “Must issue a STARTTLS command first”, SMTP authentication is likely enabled in your tenant.
  4. Try reconnecting and sending the test email again, this time with a valid Office 365 username and password configured in AUTH LOGIN per RFC 4954.
  5. If the email sends successfully, SMTP authentication is working properly.
  6. If it fails with an authentication error, there is still an issue with SMTP authentication for your tenant or user account.

This validation process using Telnet or an SMTP client is the most reliable way to test Office 365’s actual behavior and response to SMTP authentication.

Some additional tips for testing SMTP authentication:

  • Try connecting on ports 25, 587, and 465 to test different configurations. Port 587 is typically used for TLS-encrypted SMTP.
  • Validate behavior for both internal and external IP addresses if you will be sending from multiple networks.
  • Test with both user accounts and shared mailboxes to confirm consistent behavior.
  • Capture SMTP banner messages and error codes to troubleshoot failures.
  • Review Office 365 SMTP logs after testing to verify authentication attempts.

Thorough testing will help identify any inconsistencies in SMTP authentication across your Office 365 tenant and confirm it is working as expected before making any changes.

Enabling SMTP Authentication in Office 365

If your testing reveals that SMTP authentication is not enabled for your Office 365 organization or for particular user accounts as required, you can enable it through the following methods:

Enabling SMTP Authentication for the Entire Organization

The easiest way to enable SMTP authentication globally is through the Office 365 admin center:

  1. Sign in to the Office 365 admin center.
  2. Navigate to Settings > Org settings.
  3. Find the Modern Authentication section and expand the card.
  4. Check the box to enable Authenticated SMTP. Enable SMTP Authentication in Office 365
  5. Click Save changes at the bottom of the screen.

This will enable SMTP authentication for all users in your Office 365 organization. Keep in mind existing SMTP connections may stop working until reconfigured to authenticate.

Alternatively, you can use Exchange Online PowerShell to enable organization-wide SMTP authentication with:

Set-OrganizationConfig -SMTPAuthEnabled $true

Enabling SMTP Authentication for Individual Mailboxes

To enable or disable SMTP authentication for specific mailboxes rather than the entire organization:

  1. Go to Users > Active Users in the Office 365 admin center.
  2. Click into the desired user account where you want to modify SMTP authentication.
  3. Select the Mail tab.
  4. Under Email Apps, click Manage email apps.
  5. Check or uncheck the Authenticated SMTP box.
  6. Click Save changes to apply the setting.

Repeat this process to toggle SMTP authentication for any individual Office 365 mailboxes, without affecting others.

For individual mailboxes, you can also use PowerShell by running:

#Enable SMTP AUTH for a mailbox
Set-CASMailbox -Identity [email protected] -SmtpClientAuthenticationDisabled $false

#Disable SMTP AUTH for a mailbox
Set-CASMailbox -Identity [email protected] -SmtpClientAuthenticationDisabled $true 

The Set-CASMailbox cmdlet allows precise control over SMTP authentication at the mailbox level.

Enabling SMTP Authentication for Shared Mailboxes

Shared mailboxes have SMTP authentication disabled by default. To enabled it:

  1. Go to Shared mailboxes in the Office 365 admin center.
  2. Click into the specific shared mailbox you want to update.
  3. Go to the Mail tab and click Manage email apps.
  4. Check the Authenticated SMTP box.
  5. Click Save changes.

Alternatively, use this PowerShell command to enable SMTP authentication for a shared mailbox:

Set-CASMailbox -Identity [email protected] -SmtpClientAuthenticationDisabled $false

With a few simple clicks in the Office 365 admin center or PowerShell commands, you can enable SMTP authentication globally across your organization or for individual mailboxes and shared mailboxes as needed.

Sending Emails via SMTP Authentication

Once enabled, here is how you can configure applications and devices to send mail leveraging Office 365’s SMTP authentication:

Authenticating Client Connections with SASL XOAUTH2

When applications and devices connect to Office 365’s SMTP server, they need to authenticate using the SASL XOAUTH2 protocol rather than basic authentication.

This encodes the username and OAuth access token issued by Azure AD into a base64 string sent with the AUTHENTICATE XOAUTH2 command like:

AUTHENTICATE XOAUTH2 [base64 encoded string containing username and access token]

Office 365 then decodes and validates this information to authenticate the client before allowing the email to be sent.

Microsoft has guidelines and code samples for implementing SASL XOAUTH2 authentication when connecting to Office 365 SMTP.

Sending Emails from Applications and Devices

Here are a couple options for sending mail via Office 365’s SMTP server leveraging authentication:

H4: Via Direct Send

For applications on your network, you can configure “direct send” to Office 365’s MX record as the target SMTP server. This doesn’t require Office 365 credentials.

Make sure to allow the device’s IP in your SPF record and consider creating an inbound mail connector if you need to send to external recipients.

H4: Via Inbound Connectors

For on-premises servers that need to authenticate when sending mail, create an Office 365 inbound connector specifying the allowed IP address.

Have your application authenticate to the SMTP server using credentials for a service account or shared mailbox with send permissions. This will relay mail through Office 365’s SMTP authentication.

With some strategic planning, you can securely send both internally and externally from applications via Office 365 SMTP authentication

Troubleshooting Issues with SMTP Authentication

If you are having issues sending email after enabling Office 365 SMTP authentication, here are some troubleshooting tips:

Checking for Common SMTP Error Codes

Review Office 365 SMTP logs and banners for common SMTP error codes like:

  • 535 – Indicates a general authentication failure
  • 535 5.7.3 – Authentication unsuccessful
  • 535 5.7.139 – Authentication unsuccessful, account not configured for SMTP

These types of errors indicate a problem authenticating and connecting to Office 365’s SMTP server that needs to be corrected.

Some other error codes to watch for:

  • 530 – Access denied due to invalid credentials
  • 534 – Authentication failed
  • 454 – TLS not available due to temporary reason

Capturing the exact SMTP error codes and messages will help narrow down the problem.

Verifying Azure AD and Exchange Online Configurations

There are a few key things to check in Azure AD and Exchange Online that could cause SMTP authentication issues:

In Azure AD:

  • Confirm the account has an Office 365 or Exchange Online license assigned. Accounts without proper licenses will fail to authenticate.
  • Verify multi-factor authentication is not enabled on the account if using basic authentication. MFA will break basic auth.
  • Check that Application Impersonation delegation rights are properly configured if authenticating via a service principal.

In Exchange Online:

  • Validate the mailbox or shared mailbox has SMTP AUTH enabled and proper permissions configured.
  • Ensure there are no inbox rules, transport rules, or other policies blocking SMTP AUTH.
  • Check for Exchange Online PowerShell session expiration which can cause auth failures.

Incorrect configuration in Azure AD or Exchange Online can prevent successful SMTP authentication.

Using Telnet to Test SMTP Connectivity

You can also use Telnet to manually connect and test the Office 365 SMTP server:

  1. Connect to smtp.office365.com on port 587 using Telnet or OpenSSL.
  2. Attempt to send a test email without authentication.
  3. Try sending the email again using SMTP AUTH with valid Office 365 credentials.
  4. Capture any error codes, banners, or diagnostic info returned by Office 365.
  5. Use a tool like Wireshark to inspect the SMTP traffic and authentication attempts.

This low-level testing can help identify the exact stage at which authentication is failing.

Microsoft Remote Connectivity Analyzer

Microsoft also provides the Remote Connectivity Analyzer that can test SMTP authentication and connectivity to Office 365.

It will validate DNS records, perform an SMTP EHLO/HELO test, attempt an SMTPAUTH login, and confirm TLS availability.

The detailed results can help diagnose many common SMTP configuration issues.

SMTP Authentication Diagnostics Tool

For additional troubleshooting, Microsoft provides an SMTP authentication diagnostics tool that can:

  • Automatically detect Exchange Online misconfigurations
  • Check for disabled accounts or passwords
  • Validate proper licenses are assigned
  • Test end-to-end email delivery

The diagnostics tool can save time troubleshooting and identify potential issues.

With some targeted troubleshooting, you can resolve most Office 365 SMTP authentication issues and restore email sending capabilities.

Best Practices for SMTP Authentication in Office 365

To securely take advantage of Office 365’s SMTP authentication capabilities, be sure to follow these recommended best practices:

Limiting Access to Shared Mailboxes

Shared mailboxes have SMTP authentication disabled by default in Office 365. If enabling SMTP authentication for shared mailboxes, be extremely restrictive in granting Send As permissions.

Only assign Send As rights on shared mailboxes to user accounts that absolutely require it. The more accounts that have SMTP authentication enabled, the greater the potential for abuse if credentials are compromised.

Regularly review the list of accounts with Send As permissions and remove any that are no longer needed. Approve new Send As assignments to shared mailboxes through a change management process.

Monitoring Usage and Access Logs

Actively monitor Office 365 sign-in, audit, and SMTP authentication logs to detect anomalies that could indicate compromised credentials or abuse of SMTP authentication.

Watch for unusual spikes in daily SMTP authenticated connection counts, irregular sending patterns, or logins from suspicious IP addresses. Enable Office 365 anomalous activity reports for proactive alerts.

Some key Office 365 log sources to review include:

  • Sign-ins – Identify suspicious IP addresses, user agents, and geographic locations.
  • Audit logs – Review for spikes in Send As permissions granted.
  • Mailbox audit logs – Detect unusual sending patterns from shared mailboxes.
  • SMTPAUTH logs – Monitor daily authenticated SMTP connection counts.
  • Mail flow reports – Uncover spikes in outbound sending volumes.

Log all SMTP authentication activity and feed logs into a security information and event management (SIEM) platform for advanced analysis if possible.

Enforcing Multi-Factor Authentication

For accounts with elevated Send As permissions on shared mailboxes, enforce multi-factor authentication through Conditional Access policies in Azure Active Directory:

  1. Create a Conditional Access policy to require MFA for configured users. Require MFA for Send As accounts
  2. Assign the policy to security groups containing accounts with Send As rights.

Requiring MFA reduces the risk of unauthorized access if credentials are compromised, adding an extra layer of security for SMTP authentication.

Updating Applications to Support Modern Authentication

Configure on-premises and custom applications to use modern authentication mechanisms like SASL XOAUTH2 rather than basic authentication when connecting to Office 365 SMTP.

For example, leverage the Microsoft Identity Platform and Azure AD to obtain OAuth access tokens for authenticating SMTP sessions.

Using newer protocols like SASL XOAUTH2 enhances security and provides detailed logging in Azure AD of all SMTP authentications rather than just username/password verification.

Limiting Daily Send Limits

To limit potential abuse of authenticated SMTP, create an Exchange Online mail flow rule to enforce organization-wide send limits:

  1. In the Exchange Admin Center (EAC), navigate to Mail flow > Rules.
  2. Click New [+] > Create a new rule.
  3. Give the rule a name like “Enforce Daily Send Limits”.
  4. Set conditions to match all messages from any sender.
  5. For actions, select Throttle message delivery:
    • Type: Per user
    • Maximum messages per 24 hours: 500
    • Reject message if limit exceeded: Yes
  6. Save and apply the rule.

This will throttle Office 365 SMTP usage at an organization level as an added protection. Granular send limits can also be configured on inbound mail connectors.

Automating Account Lockouts

Use Azure AD Identity Protection to automatically disable accounts or require password changes if suspicious SMTP authentication activity is detected:

  1. Enable Identity Protection in the Azure AD admin center.
  2. Configure risk-based policies like:
    • Sign-in risk – Require password change if high-risk authentication detected.
    • User risk – Automatically disable user accounts with high aggregated risk level.
  3. Review and tune detection settings to avoid false positives.

Automatically locking accounts or forcing password resets can prevent continued abuse after credentials are compromised.

Securing Legacy Protocols

Disable legacy authentication protocols like POP3, IMAP, and SMTP in your Exchange Online tenant. Only enable legacy protocols if critically required for compatibility reasons:

Set-OrganizationConfig -OAuth2ClientProfileEnabled $false

Legacy protocols use basic authentication, which transmits credentials in plain text. Disable to enhance security.

Following Microsoft Recommendations

Review Microsoft’s best practice guides for Office 365 and Exchange Online mail flow and connectivity for additional recommendations to harden your tenant.

Some key security areas to optimize include identity, permissions management, logging, endpoints, network architecture, and more.

Key Takeaways

Optimizing and securing SMTP authentication in Office 365 provides organizations with significant benefits but requires careful planning and configuration. Some key takeaways:

  • SMTP authentication prevents unauthorized use of Office 365’s mail servers by requiring valid credentials for outbound sending. This protects against spam and abuse.
  • Validate that SMTP authentication is enabled properly across your Office 365 tenant by checking settings and directly testing functionality.
  • Enable SMTP authentication globally or selectively at the mailbox-level based on your requirements. Shared mailboxes have it disabled by default.
  • Configure applications to authenticate using modern mechanisms like SASL XOAUTH2 rather than basic authentication.
  • Watch for SMTP errors like 535 and 530 codes that indicate problems authenticating. Use tools like Telnet and Microsoft’s SMTP diagnostic to troubleshoot issues.
  • Limit Send As permissions on shared mailboxes, enforce multi-factor authentication for privileged accounts, monitor usage closely, and frequently review recommendations.
  • Following Microsoft’s deployment best practices for Office 365 SMTP authentication allows organizations to maximize security and prevent abuse while enabling critical mail flow.

With proper planning, validation, and monitoring, Office 365’s SMTP authentication capabilities allow organizations to securely route outbound email from both cloud and on-premises applications.

Frequently Asked Questions

Here are answers to some frequently asked questions about configuring and troubleshooting SMTP authentication in Office 365:

Q: Is SMTP authentication enabled by default in Office 365?

A: Microsoft enables SMTP auth by default for most Office 365 tenants, but organizations created before 2020 may still have it disabled. Always verify status in your specific tenant.

Q: How do I check if SMTP authentication is enabled in my Office 365 tenant?

A: Check the Office 365 admin center under Org Settings > Modern Authentication. You can also test functionality directly using Telnet or SMTP connection tools.

Q: How do I enable SMTP authentication globally at the organization level?

A: Go to Org Settings > Modern Authentication in the Office 365 admin center and check the box to enable Authenticated SMTP. This can also be enabled in PowerShell.

Q: How do I enable or disable SMTP authentication for specific mailboxes?

A: Edit the user in the Office 365 admin center, go to Mail settings > Manage email apps, and check or uncheck the Authenticated SMTP box. This can also be configured using the Set-CASMailbox PowerShell cmdlet.

Q: How do I enable SMTP authentication for Office 365 shared mailboxes?

A: SMTP authentication is disabled by default for shared mailboxes. Enable through the admin portal or via Set-CASMailbox in PowerShell by specifying the shared mailbox name.

Q: Why am I getting SMTP 535 authentication errors in Office 365?

A: SMTP 535 errors generally indicate authentication failure. Check that the account is licensed properly, has SMTP AUTH enabled, and is using the correct protocols and credentials in applications.

Q: How do I update my applications to use more secure modern authentication?

A: Configure applications to authenticate using SASL XOAUTH2 rather than basic authentication. This uses OAuth tokens from Azure AD for authentication and enhanced security.

Q: What are some best practices to secure SMTP authentication?

A: Limit Send As permissions, enforce MFA on privileged accounts, actively monitor usage and audit logs, use dedicated service accounts, and follow all of Microsoft’s administrative guidance.

Q: How can I troubleshoot SMTP authentication issues in Office 365?

A: Review Office 365 SMTP logs and error codes, validate Azure AD and Exchange Online configurations, use Telnet for SMTP testing, and leverage Microsoft’s connectivity tools and diagnostics utilities.