Setup SQL Server Database Mail to use a Gmail, Hotmail, or Outlook account

By:   |   Updated: 2019-10-22   |   Comments (47)   |   Related: 1 | 2 | 3 | > Database Mail


Problem

One great feature of SQL Server is the ability to get alerts when there are issues. The alert process can send email notifications using Database Mail after you have configured your SMTP settings and setup your jobs and alerts to use this account. In some cases, you may not have a mail server, but still want to setup alerts. In this tip we will walk through how you can setup Database Mail to use email services like Gmail, Hotmail or Outlook.

Solution

In this demo, we will use details specific to a Hotmail account in order to test the Database Mail configuration.

Configuring SQL Server Database Mail to use Hotmail

If you are not familiar with setting up Database Mail you can refer to this link. This will work for SQL Server 2005 and later.

When navigating through the Database Mail configuration wizard, you will encounter the below screen wherein the relevant details need to be entered. Let's consider the example of using the SMTP server details for Hotmail. Enter the details as shown below.

setup new database mail account

Outgoing Mail Server (SMTP)

  • E-mail Address - Enter your Hotmail or Live account
  • Server Name - Enter as shown in screenshot
  • Port number - Enter as shown in screenshot
  • The server requires a secure connection (SSL) - check this box

    If this is left unchecked you will encounter this error message, 'The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 4 (2011-12-14T23:36:13). 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.0 Must issue a STARTTLS command first).'

SMTP Authentication

  • Select 'Basic authentication'
  • User Name - Enter your Hotmail or Live account
  • Password - Enter password of your Hotmail or Live account.
  • Confirm Password - Enter password of your Hotmail or Live account.

The below table outlines the necessary SMTP server details and port numbers for Hotmail, Gmail and Outlook.

Mail Service SMTP Server Port Number
Hotmail smtp.live.com 587
Gmail smtp.gmail.com 587
Outlook smtp.office365.com 587

Sending a Test Email Using SQL Server Database Mail

Once your Database Mail profile has been configured you can send test emails for validation to verify setup is configured correctly. Right click on Database Mail and select "Send Test E-Mail" as shown below.

send test database mail

You could also execute the Database Mail system procedure sp_send_dbmail with the appropriate parameters using this sample script below.

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'Enter valid database profile name',
    @recipients = 'Enter Valid Email Address',
    @body = 'This is a test email sent from TEST server',
    @subject = 'TEST EMAIL',
    @importance ='HIGH'

Gmail Additional Steps Required when using SQL Server Database Mail

Database Mail works fine using the Hotmail or Outlook SMTP server details. However, an additional step is required to get Gmail to work correctly.

Gmail automatically raises a security flag when the Gmail SMTP server details is used for this purpose. Below is a screenshot of the alert generated when I used the Gmail SMTP server details.

gmail blocked for linked account

You can also review additional information about this on the Database Mail logs. You can get this information by right clicking on the Database Mail feature and selecting the "View Database Mail Log" option as shown below.

view database mail log

In order to fix the issue with Gmail, you need to enable the option to "Allow less secure apps". This is a setting in your Gmail account that needs to be enabled.

enable gmail allow less secure apps

Once this Gmail setting is enabled, Gmail SMTP server will work as expected.

Database Mail Troubleshooting

After testing, if you are unable to receive notification emails, you could use the below Database Mail views for troubleshooting.

Find successfully sent email

This contains one row for each message successfully sent by Database Mail.

select * from msdb.dbo.sysmail_sentitems

Find unsent email

This contains one row for each message that has an unsent or retrying status.

select * from msdb.dbo.sysmail_unsentitems

Find failed email attempts

This contains one row for each message that has a failed status.

select * from msdb.dbo.sysmail_faileditems

Additional options

Here are some additional views sysmail_event_log and sysmail_allitems.

There is also a Database Mail system stored procedure msdb.dbo.sysmail_help_queue_sp which could be used as well.

For detailed steps on troubleshooting Database Mail issues, refer to this link.

Summary

The above steps were performed on SQL Server 2019 Developer edition and I tested successfully using my Gmail, Hotmail and Outlook accounts, but this should work with SQL Server 2005 and later.

Note: If your organization has an SMTP server, it is recommended to use your company’s SMTP server and only use these examples where you don't have access to an SMTP server or if you need to test Database Mail.

Next Steps
  • Get familiar with the Database Mail features, you can refer to this link for more information.
  • Try testing this tip in your own test environment using your personal Hotmail, Gmail or Outlook accounts.


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Mohammed Moinudheen Mohammed Moinudheen is a SQL Server DBA with over 6 years experience managing production databases for a few Fortune 500 companies.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips


Article Last Updated: 2019-10-22

Comments For This Article




Monday, June 5, 2023 - 9:40:05 AM - Kuris Back To Top (91250)
There is no more choice, to allow less secure apps in google account (Gmail).

It is better to use as mailing system your own business mail system (usually M365), or maybe hotmail, or outlook.com.

https://support.google.com/accounts/answer/6010255?authuser=1&hl=cs&authuser=1&visit_id=21685972095170-5312077264024472898&p=less-secure-apps&rd=1


Thursday, April 28, 2022 - 10:10:58 AM - bharath Back To Top (90042)
Today I received mail subjected "You may lose access to some of your third-party apps" saying google is stopping less secure option from 30th May to continue using we need to re login as follows; To help keep your account secure, Google will no longer support the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password. Instead, you’ll need to sign in using Sign in with Google or other more secure technologies, like OAuth 2.0. So how to use google account as database emailer at MSSQL with new security upgrade?

Thursday, April 29, 2021 - 4:32:08 PM - Vladimir Filipović Back To Top (88627)
Thanks for the help

Wednesday, October 2, 2019 - 9:30:44 AM - lakshay arora Back To Top (82648)

I tried the methods shown in the link. However, stiil getting error while sending mail from sql server to my gmail account.

Error:- Cannot send mail. Mail server failure.

Please help me out.

Thanks in advance!! :))


Monday, March 12, 2018 - 6:19:35 PM - John parker Back To Top (75394)

This is the best article because I have sent my first database with the help of your article.  Now I can easily use a Gmail, Hotmail, Yahoo or AOL account and accessing two accounts at a single time.


Friday, December 22, 2017 - 3:13:44 AM - Kamil Back To Top (74320)

 Hello Mohammed, 

 Thank you for a very helpful guide - I have tried other sites and this is the first one I found clearly stating what's required and I have finally sent my first database email.

 


Wednesday, December 6, 2017 - 4:08:45 PM - Nick Back To Top (73696)

Worked like a charm using Gmail on the first try, extraordinarily helpful, thank you!


Sunday, June 4, 2017 - 10:07:54 PM - JeffreyR Back To Top (56740)

I was able to get Database Mail working via Gmail, but hit one road bump.  So to help others, here is what I had to do.

Gmail apparently doesn't think much of the security protocols used by SQL Server's Database Mail.  By default, it will reject messages from Database Mail, and return the following error which you can see in the sysmail_event_log:

"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."

To fix this you have to configure Gmail to accept connections from "less secure apps" under My Account.   See the following link for details.

https://support.google.com/accounts/answer/6010255?hl=en&authuser=1 


Sunday, January 10, 2016 - 5:02:50 PM - SAURABH Back To Top (40385)

 

 I am able to send the mail using above setting in SSMS while when using the same SMTP  (smtp.live.com) in SSIS (send mail task), it throughing below error message.

[Send Mail Task] Error: An error occurred with the following error message: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first".

 

Please help.


Tuesday, November 24, 2015 - 4:20:37 PM - Wayne Back To Top (39134)

I've configured both Yahoo Mail and Gmail for this purpose: I'd definitely recommend Gmail.  With it, emails are delivered pretty much instantly, Yahoo Mail seems to cache.  If I have my DBCCs scheduled to run at 23:59, then I'll receive an email from Gmail at 23:59.  With Yahoo, there's no telling when it'll arrive.

I haven't used Hotmail or AOL for this purpose, so I can't comment on them.


Monday, June 1, 2015 - 5:32:59 AM - Noor Alam Back To Top (37344)

 

How tp enable database mail feature in sql server 2008 r2..

i di'nt find database mail in under managemant.....

 

pls.. answer asap

 


Monday, April 6, 2015 - 4:00:46 AM - Sam Back To Top (36833)

Excellent article, keep it up


Monday, February 23, 2015 - 2:37:51 AM - pankaj Back To Top (36311)

Hi how can i send file Attachments from one server to another server.


Thursday, September 11, 2014 - 1:04:59 AM - venkatesh Back To Top (34485)

SRY PLS GO THROUGH THIS

 

You can switch to an app made by Google such as Gmail to access your account (recommended) or change your settings at https://www.google.com/settings/security/lesssecureapps so that your account is no longer protected by modern security standards.


Thursday, September 11, 2014 - 1:03:28 AM - venkatesh Back To Top (34484)

The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2013-09-22T10:56:05). Exception Message: Could not connect to mail server. (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 106.10.150.171:25).

PLS GO THROUGH THIS AND ENABLE IT

 

 

Please review your Account Activity page at https://security.google.com/settings/security/activity to see if anything looks suspicious. Whoever tried to sign in to your account knows your password; we recommend that you change it right away.


Thursday, September 11, 2014 - 12:38:06 AM - venkatesh Back To Top (34483)

hiii please respond to this error.

The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 4 (2014-09-11T09:28:38). 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.5.1 Authentication Required. Learn more at).
)


Sunday, September 22, 2013 - 1:29:34 AM - SQLDBA Back To Top (26888)

hi,

 

The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2013-09-22T10:56:05). Exception Message: Could not connect to mail server. (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 106.10.150.171:25).
)

 

 


Sunday, September 22, 2013 - 1:16:36 AM - SQLDBA Back To Top (26887)

Hi,

 

The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2013-09-03T23:30:45). 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.5.1 Authentication Required. Learn more at).
)


Sunday, September 22, 2013 - 1:14:48 AM - SQLDBA Back To Top (26886)

Hi,

I AM getting following error.

The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2013-09-03T23:19:40). 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.0 Must issue a STARTTLS command first. xl3sm23771591pbb.17 - gsmtp).
)

 

 


Thursday, August 29, 2013 - 7:22:58 AM - vys Back To Top (26522)

 

Thanks , Nice article ,its helped me a lot - keep updating 


Tuesday, August 20, 2013 - 2:54:28 AM - SQLDBA Back To Top (26384)

Hi,

Please check following error come.

The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2013-08-20T12:17:17). Exception Message: Could not connect to mail server. (No connection could be made because the target machine actively refused it 173.194.79.108:587).
)

 

Regards,

SQLDBA


Wednesday, July 10, 2013 - 2:27:16 AM - sainadh Back To Top (25771)

Superb article.. 


Wednesday, April 17, 2013 - 5:44:37 PM - karthik Back To Top (23406)

Hi,

I have scheduled Database backup by using sql server management plan for the time mentioned. The backup is started & completed in the given time. Once the backup is completed i wanted to send email alert that backup is completed to the specific account. Can you please guide me what are the steps that I need to configure this activity. 

Please send me the details how to do this activity as soon as possible.

My email address is [email protected]

 


Tuesday, July 31, 2012 - 3:34:50 AM - sandeep Back To Top (18857)

Thanks its working your explanation is so clear thanks for posting


Wednesday, July 25, 2012 - 1:26:22 AM - Nauman Ikram Back To Top (18791)

Sir, if I configured any gmail or hotmail account, I only want to get my emails with their attachments in sql server. Furthere I want to parse the attachment and save the data in database tables.

Is it possible to get the mails in sql server ?

 


Tuesday, July 3, 2012 - 1:53:58 AM - Jit Back To Top (18317)

 

Ya I Restarted SQL Server Agent Also. But Still the same error


Monday, July 2, 2012 - 11:55:22 PM - Mohammed Moinudheen Back To Top (18314)

@JIT,

Did you try restarting the SQL Server Agent?


Monday, July 2, 2012 - 12:28:21 PM - JIT Back To Top (18304)

 

I Configured Database Mail.It worked fine then our organization changed the smtp server.

After Configuring Database Mail With New smtp Server , Database Mail Log is Showing Error

Exception Message: Cannot send mails to mail server. (Failure sending mail.)

We Are Using Port 465  & I tried telnet also.It is working fine but still database mails are failed

Plz help me its very urgent !!!!!!!!!

 


Monday, July 2, 2012 - 10:13:54 AM - john the rule Back To Top (18299)

Super ...........


Thursday, May 17, 2012 - 4:57:40 AM - Ganpat Sharma Back To Top (17495)

 

Hi everyone. i am sending email from sql server 2008 r2.   database mail configuration details set as : -------------------------------------------------------------- Server Name : smtp.gmail.com Port No : 587 Set Basic Authentication : User Name, Password   i tried more but email sending is failed and error occur is-:   The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 2 (2012-05-03T15:46:30). Exception Message: Could not connect to mail server. (An attempt was made to access a socket in a way forbidden by its access permissions 173.194.79.108:587). )   Thanks and Regards: Ganpat Sharma

Sunday, March 11, 2012 - 10:42:27 AM - Ameer Khan Back To Top (16337)

Good Article Moin


Friday, March 9, 2012 - 11:20:30 AM - Mohammed Moinudheen Back To Top (16320)

Ron,

@profile_name refers to your valid mail profile. You can get details of the available profiles in your instance by executing this procedure.

EXECUTE msdb.dbo.sysmail_help_profile_sp;

Thanks


Friday, March 9, 2012 - 9:36:57 AM - ron Back To Top (16317)

What does " @Profile_name " refer to in your sample script?

thanks


Wednesday, January 11, 2012 - 7:27:58 AM - Srinath Back To Top (15607)

Thank you for such a Very nice article...Keep going Moinu...!!


Wednesday, January 11, 2012 - 6:56:35 AM - bojanna mk Back To Top (15606)

Good article Moinu.Really Intersting!


Monday, January 9, 2012 - 11:53:29 AM - Patel H Back To Top (15560)

Its McFee -  I know I wouldnt remove antivirus on my work machine....I think I have answered my own question...but thanks..


Monday, January 9, 2012 - 11:18:28 AM - Ankit Shah Back To Top (15558)

nice article which i was looking for to setup on my local machine and you comes out with this.

Great.

thanks


Monday, January 9, 2012 - 8:13:34 AM - Mohammed Moinudheen Back To Top (15551)

@Patel: Just to remind, I would not recommend to change anything on your company laptop.


Monday, January 9, 2012 - 8:10:57 AM - Mohammed Moinudheen Back To Top (15550)

@Patel: I guess due to this antivirus setting you would face this error irrespective of whether you are using your yahoo, gmail or hotmail accounts. By the by, which anti virus are you using. You might require to disable this port blocking rule.


Monday, January 9, 2012 - 8:09:25 AM - Patel H Back To Top (15549)

I get same anti virus error mentined in my last comment even when trying with hotmail..


Monday, January 9, 2012 - 7:45:59 AM - Patel H Back To Top (15548)

Blocked by port blocking rule (Anti-virus Standard Protection:Prevent mass mailing worms from sending mail).  -  This error I see in my db mail logs which maks me think that anti virus on my machine may be blocking this...

is that so??

If not what the other ways of getting round using yahoo mail plz?


Monday, January 9, 2012 - 7:01:48 AM - Mohammed Moinudheen Back To Top (15547)

@Rajendra:Thanks for your comments

@Alex: I agree, Thanks for your comments.

@Jason: Thanks for mentioning this link:http://ypopsemail.com. While testing I had used only hotmail and gmail. So I was not aware about the problem faced by Patel when he used his yahoo account.

 


Saturday, January 7, 2012 - 1:51:47 PM - Jason Yousef Back To Top (15541)

You need to pay to get a smtp access for yahoo. unless you use http://ypopsemail.com/.

 


Saturday, January 7, 2012 - 12:00:13 PM - Patel H Back To Top (15539)

I tried using yahoo account but get below error in the database mail logs. 

 

select

*from msdb.dbo.

sysmail_faileditems  - can see here the unsent messages..


Message
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2012-01-07T16:55:58). Exception Message: Could not connect to mail server. (An attempt was made to access a socket in a way forbidden by its access permissions 98.139.212.139:25).

I am not sure what is missing..as I have done exacty what been said in the article..plz advise..

 


Friday, January 6, 2012 - 10:31:14 AM - Alen Teplitsky Back To Top (15536)

nice article but a potential security hole. if you open up outbound smtp from any endpoint behind your firewall then hackers/spammers can use this to send out spam.


Friday, January 6, 2012 - 8:08:01 AM - Jason Yousef Back To Top (15533)

Nice one, Thanks!


Friday, January 6, 2012 - 7:59:20 AM - rajendra Back To Top (15532)

Thanks for the Tip.















get free sql tips
agree to terms