solving sql server problems for millions of dbas and developers since 2006



SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Tip Categories SQL Server Tutorials SQL Server Webcasts SQL Server Whitepapers SQL Server Tools SQL Server Questions and Answers MSSQLTips Authors About MSSQLTips SQL Server User Groups MSSLQTips Giveaways MSSQLTips Advertising Options

MSSQLTips Facebook Page MSSQLTips LinkedIn Page MSSQLTips RSS Feed MSSQLTips Twitter Page MSSQLTips Google+ Page

SQL Product Highlight

Idera - SQL safe backup

Idera's SQL safe provides a high-performance backup and recovery solution for Microsoft SQL Server. SQL safe saves money by reducing database backup time by up to 50% over native backups and reducing backup disk space requirements by up to 95%. SQL safe also enables complete "hands-free" automated backup of your entire SQL Server infrastructure and ensures compliance with your organization's backup and recovery policies. From implementations with tens of SQL servers to enterprises with hundreds of servers spread around the globe, SQL safe is the only SQL Server backup and recovery solution that scales to meet the challenge.

Learn more!








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

By: | Read Comments (18) | Print

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

Related Tips: More

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, Yahoo, etc...

Solution

For this example, I have a SQL Server test environment configured and I want to test the alert mechanism using Hotmail. The following outlines the settings to do this.

Configuring Database Mail

If you are not familiar with setting up Database Mail you can refer to this link: http://www.mssqltips.com/sqlservertip/1100/setting-up-database-mail-for-sql-2005/.  This will work for later versions of SQL Server as well.

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

setup database mail wizard

Under '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).'

Under '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, Yahoo and AOL (see this for more info http://support.microsoft.com/kb/2352963).

SMTP Server Details

Hotmail        SMTP server name: smtp.live.com                    Port number:   587
Gmail           SMTP server name: smtp.gmail.com                  Port number:   587
Yahoo          SMTP server name: smtp.mail.yahoo.com          Port number :  25
AOL             SMTP server name: smtp.aol.com                    Port number :  587

Here are some additional links you can refer to that show how to configure Microsoft Outlook to connect to Gmail, Hotmail etc... which is similar to what needs to be done for Database Mail.

Sending a Test Email

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 database mail test email from ssms

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'

Database Mail Troubleshooting

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

select * from msdb.dbo.sysmail_sentitems 

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

select * from msdb.dbo.sysmail_unsentitems 

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

select * from msdb.dbo.sysmail_faileditems 

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

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: http://msdn.microsoft.com/en-us/library/ms188663.aspx

Summary

The above steps were performed using SQL Server 2008 R2 and I tested using Hotmail and Gmail.  This should work for any version of SQL Server 2005 and greater.

Note: If your company has an SMTP server I strongly urge that you use your companies SMTP server and only use this where you don't have access to an SMTP server or if you need to test Database Mail.

Next Steps



Related Tips: More | Become a paid author


Last Update: 1/6/2012

Share: Share 






Comments and Feedback:

Friday, January 06, 2012 - 7:59:20 AM - rajendra Read The Tip

Thanks for the Tip.


Friday, January 06, 2012 - 8:08:01 AM - Jason Yousef Read The Tip

Nice one, Thanks!


Friday, January 06, 2012 - 10:31:14 AM - Alen Teplitsky Read The Tip

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.


Saturday, January 07, 2012 - 12:00:13 PM - Patel H Read The Tip

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..

 


Saturday, January 07, 2012 - 1:51:47 PM - Jason Yousef Read The Tip

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

 


Monday, January 09, 2012 - 7:01:48 AM - Mohammed Moinudheen Read The Tip

@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.

 


Monday, January 09, 2012 - 7:45:59 AM - Patel H Read The Tip

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 09, 2012 - 8:09:25 AM - Patel H Read The Tip

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


Monday, January 09, 2012 - 8:10:57 AM - Mohammed Moinudheen Read The Tip

@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 09, 2012 - 8:13:34 AM - Mohammed Moinudheen Read The Tip

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


Monday, January 09, 2012 - 11:18:28 AM - Ankit Shah Read The Tip

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

Great.

thanks


Monday, January 09, 2012 - 11:53:29 AM - Patel H Read The Tip

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


Wednesday, January 11, 2012 - 6:56:35 AM - bojanna mk Read The Tip

Good article Moinu.Really Intersting!


Wednesday, January 11, 2012 - 7:27:58 AM - Srinath Read The Tip

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


Friday, March 09, 2012 - 9:36:57 AM - ron Read The Tip

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

thanks


Friday, March 09, 2012 - 11:20:30 AM - Mohammed Moinudheen Read The Tip

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


Sunday, March 11, 2012 - 10:42:27 AM - Ameer Khan Read The Tip

Good Article Moin


Thursday, May 17, 2012 - 4:57:40 AM - Ganpat Sharma Read The Tip

 

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


Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
Sponsor Information
"SQL doctor is the best SQL product on the market, by far. All of Idera's tools are great, but this is the icing on the cake!"

The 10 tools in the SQL Developer Bundle cut the time spent in dull and tedious tasks. Learn more.

SQL Servers keeping you up at night? Contact the Edgewood SQL Server consultants for a Health Check.

Get SQL Server Tips Straight from Kevin Kline.

Solving SQL Server problems for millions of DBAs and Devs since 2006. Join now.

Are you waiting on SQL Server? Learn about these DMV's.


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com