Problem Diagnosis
Start by diagnosing DatabaseMail issues via SSMS with SQL Server > Management > Database Mail (right-click) > View Database Mail Log. Example error messages you're likely to see include:
The mail could not be sent to the recipients because of the mail server failure. (...
Exception Message: Cannot send mails to mail server. (
Failure sending mail.
).
)
This, unfortunately, is a very generic error message. It probably means that your local server, .NET Framework, or the DatabaseMail.exe process itself has not yet been configured to enable the TLS 1.2 protocol, so is failing to connect using TLS 1.0 or TLS 1.1 protocols.
The mail could not be sent to the recipients because of the mail server failure. (...
Exception Message: Cannot send mails to mail server. (
Error in processing. The server response was: 5.7.3 STARTTLS is required to send mail [FOO.BAR.prod.outlook.com]
).
)
This means that "This server requires a secure connection (SSL)" has not been ticked. This must be ticked to enable the STARTTLS command that establishes a secure communications channel over which SMTP Basic authentication gets sent.
The mail could not be sent to the recipients because of the mail server failure. (...
Exception Message: Cannot send mails to mail server. (
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, SmtpClientAuthentication is disabled for the Tenant. Visit https://aka.ms/smtp_auth_disabled for more information. [FOO.BAR.prod.outlook.com]
).
)
This means that the Office365 mailbox being used in the Basic authentication details has not yet had the SMTP AUTH property enabled on it.
SQL Server's DatabaseMail vs. smtp.office365.com requirements
- SMTP AUTH must be enabled on the mailbox of the sending account.
How to set up a multifunction device or application to send email using Microsoft 365 or Office 365 says:
You must also verify that SMTP AUTH is enabled for the mailbox being used.
SMTP AUTH is disabled for organizations created after January 2020 but can be enabled per-mailbox.
For more information, see Enable or disable authenticated client SMTP submission (SMTP AUTH) in Exchange Online.
Speak to your organization's Exchange administrator to have this setting enabled or, if you have sufficient access yourself, you can do this via PowerShell:
PS> Import-Module ExchangeOnlineManagement
PS> Connect-ExchangeOnline -UserPrincipalName administrative_user@your_domain.com
PS> Get-CASMailbox -Identity sending_mailbox_user@your_domain.com
Name ActiveSyncEnabled OWAEnabled PopEnabled ImapEnabled MapiEnabled SmtpClientAuthenticationDisabled
---- ----------------- ---------- ---------- ----------- ----------- --------------------------------
sending_mailbox_user True True True True True
PS> Set-CASMailbox -Identity sending_mailbox_user@your_domain.com -SmtpClientAuthenticationDisabled $false
- TLS 1.2 is required.
How to set up a multifunction device or application to send email using Microsoft 365 or Office 365 also says:
Transport Layer Security (TLS): Your device must be able to use TLS version 1.2 and above.
- DatabaseMail.exe is built for .NET Framework 3.5, but you need a .NET Framework installed that supports TLS 1.2 (.NET Framework 4.5.2 or later).
- TLS 1.2 client protocol should be enabled at the machine level in Registry
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client]
"DisabledByDefault"=dword:00000000
"Enabled"=dword:00000001
- TLS 1.2 client protocol should be enabled for .NET Framework 4.x in the Registry
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001
- An appropriate supportedRuntime should be in DatabaseMail.exe.config file, e.g.: with Microsoft .NET Framework 4.5.2 installed:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DatabaseServerName" value="." />
<add key="DatabaseName" value="msdb" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
- Via the Database Mail settings in SSMS, configure the sending account appropriately:
- Server name: smtp.office365.com
- Port number: 587 (preferred, or 25)
- This server requires a secure connection (SSL): must be ticked (this enables STARTTLS)
- SMTP Authentication:
- Basic authentication (selected)
- User name: sending_mailbox_user@your_domain.com
- Password: your_office365_password
- Confirm password: your_office365_password_again
References: