SQL Server :: Why your Database Mail is not working and probably never will

Database MailI’ve passed the Microsoft Certification DP-300 (at the second try). I was surprised to see that every now and then the exam was bringing up the Database Mail feature in an exam mainly focused on Azure.

The fact that SQL Server could send e-mail it has always intrigued me and few years ago I had also created a tool aimed to send you e-mails.

But now a few years have passed, I tried to use it and “kaboom!”; nothing worked.

So let’s dive together into the meanders of this appealing feature.

First approach: using Outlook Office 365 e-mail

If I could have 1BTC for every time I saw Microsoft being incompatible with Microsoft I could be a millionaire.

And this is once again the case. If you try to setup the mail account this way:

Outlook SMTP Database Mail

You will end up with the error:

The problem is that your Outlook e-mail is using 2FA.

You can probably disable the 2FA for your Outlook e-mail if you want.

But if you want you can also detach your seat belt and expose half of your torso out of the car window while driving.

There is also the option Windows Authentication using Database Engine service credentials but if you want to go down that pattern you then have to go into the MSSQLSERVER service and change the login account from Local system account to your user.

C’mon, who could possibly setup a server this way on production?

Second attempt: using Gmail e-mail

If your company is not using Outlook there is a good chance that you are using Gmail. So I setup the e-mail account for Gmail:

Gmail SMTP Database Mail

And the result is slightly the same, 2FA is preventing us from sending e-mails:

Plus you might have received an alert on your phone saying : “Google stopped the sign-in attempt, but you should review your account’s activity.”

Third attempt: using my domain e-mail

Let’s use my domain e-mail. I don’t have 2FA for that.
My domain is stored on GreenGeeks and has SMTP port set to 465 by default:

SQL Server send e-mail port 465

At this point at least I have a new error:

This is because I’m using port 465 which has been lately deprecated.

But there’s a way out: your ISP or hosting might tell you that port 465 is the default one but that doesn’t mean that 25 or 587 aren’t available. That was my case and I was effectively able to send e-mails from both ports 25 and 587.

Conclusion

Setting up Database Mail in SQL Server is painful because you end up going back and forward thousand times. You keep creating, deleting, creating till it works but this is like playing naval battle for hours. The setup this feature is a maze, you get frustrated and is overall undignifying .

That why I created for you this T-SQL query that makes the job done at first try (if you don’t use Microsoft or Google e-mail):

And if you want to delete it:

 

Related

29 Replies to “SQL Server :: Why your Database Mail is not working and probably never will”

  • To setup Database Mail Using OFffice 365 Email user as the sender you need to us an application password and:
    • As the SMTP relay used is : SMTP AUTH client submission
    • This option is not compatible with Microsoft Security Defaults.
    • Hence we Disabled Security Defaults :
    Azure Active Directory > Click on Properties in left action pane > Scroll Down & Click on Manage Security Defaults > Toggle the button to Off
    • Now make sure Authenticated client SMTP submission (SMTP AUTH) is enabled for user.
    Go to Admin center ( https://admin.microsoft.com ) > Users > Active Users > Click on User who is sending emails via SMTP relay > Click on Mail > Manage email apps > If Authenticated SMTP is unchecked, select it and save OR If Authenticated SMTP is checked already, uncheck it save wait for 2 minutes and check it again and save it.

    • If Authenticated SMTP is checked already, uncheck it save wait for 2 minutes and check it again and save it <– and we are back in business. Thanks a lot!

  • Also to use Datbase Mail with Office 365 you need to be using TLS 1.2

    • Bonjour,

      Il semblait que le sujet de départ concerné l’envoi de mail avec Office 365,
      mais bizarre que ensuite vous présentez une solution parlant du smtp d’un hébergeur.

      Avez vous une solution pour l’utilisation de office 365 avec Database Mail??
      si oui je suis très intéressé, prière de nous la partager svp.

      Cordialement.

  • Hi
    I had the same issue with my Microsoft365 account.
    I change the Port from 587 to 25 – and I worked.
    But it depends on your firewall settings

  • THIS WAS FREAKIN’ AWESOME … spun wheels for days attempting to configure mail in SQLExpress … perfect documentation … and I’ve been in this business since 1977!!! Thank You!!!

    Mark C
    Charlotte, NC

    • What was the result? Were you using Microsoft or Gmail?
      In the end were you able to send that e-mail?

  • If Authenticated SMTP is checked already, uncheck it save wait for 2 minutes and check it again and save it <– and we are back in business. Thanks a lot!

  • My problem was my profile needed to grant sendmail permissions to my userid. I did this by creating a profile, ssms management/email/manage configuration/manage profile security and add the user name to both public and private.

    • Yep, Richard!
      And the T-SQL script at the bottom of my post is going that too. (EXECUTE msdb.dbo.sysmail_add_principalprofile_sp)

  • Thanks a bunch. I could not get SQL Server email to work for the last hour or so. I ran your script and email worked the first time I tried it. Bizarrely it stopped working after that, but did give a different error message (bad authorization or TLS needed). So I manually set the TLS and it is working fine now.

  • sql server 2019 standard fully updated
    used your script but still not working
    Outlook 2016 installed on the server works normally (same email)
    I have my own domain

    i granted permission to everyone to use the profile
    what can be the issue ?

    • Try to manually set the TLS as @Graeme said in the previous comment and let me know. Thank you

  • i have been working on it the whole day and could not resolve it. you have to add a regedit DWORD SchUseStrongCrypto with a value 1.at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft.NETFramework\v4.0.30319 this will resolve it. i hope it helps

    • Thank you @Yasir, checking the SSL-checkbox and restarting Windows wouldn’t be enough?

  • Apparently this was caused by Microsoft enforcing TLS 1.2. I tried all of the fixes outlined here but this one did the tick for me. Windows Server 2012r2.
    https://support.microsoft.com/en-gb/topic/support-for-tls-system-default-versions-included-in-the-net-framework-3-5-on-windows-8-1-and-windows-server-2012-r2-499ff5ef-a88a-128b-c639-ed038b7d2d5f

    the reason why we had to add the registry key manually is because for some reason it does not add it after the kb update.

  • HI,

    I’m getting similar error, but from using reporting services in native mode (using Office 365). I’m sharing this page with my team as I think your steps will point us in the right direction. Can you offer any SSRS specific suggestions?

    • I don’t know much about SSRS but as a first step I suggest you to use a domain e-mail and not Google/Outlook365. Debug with a normal e-mail without SSO and test if that works.

      If it works now try with Outlook365.

  • Hello,

    I used Nartac to manually set TLS 1.2 but still I cannot send an email to gmail account through SMTP server. I followed all of the other instructions and it does not work. Error: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. My Windows Update is fully updated and SQL Server is fully updated.

  • Boa tarde!
    Gostaria de agradecer ao seu conteúdo, sua solução funcionou de forma precisa.
    Obrigado!

    Hi, I want to congrats for your solution, my problem was solved.
    Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *