Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Database Mail maintenance in SQL ServerDoing UPSERTs in MongoDB »
    comments

    If you want to mess around with Database Mail from your laptop and you don't have a mail server, you can configure gmail for that purpose. It is pretty easy to setup, I will show you the screen from the wizard as well how to do it in T-SQL. I prefer T-SQL because I can run the same setup now on multiple machines

    Below is the whole thing in one easy to run script
    Make sure to change Your.Account@gmail.com to what your gmail account is, also notice that @enable_ssl =1 and that we are using port 587 (@port = '587')

    1. IF NOT EXISTS(SELECT * FROM msdb.dbo.sysmail_profile WHERE  name = 'GmailDBMail')
    2.   BEGIN
    3.    
    4.     EXECUTE msdb.dbo.sysmail_add_profile_sp
    5.       @profile_name = 'GmailDBMail',
    6.       @description  = '';
    7.   END --IF EXISTS profile
    8.  
    9.   IF NOT EXISTS(SELECT * FROM msdb.dbo.sysmail_account WHERE  name = 'GmailDBMail')
    10.   BEGIN
    11.    
    12.     EXECUTE msdb.dbo.sysmail_add_account_sp
    13.     @account_name            = 'GmailDBMail',
    14.     @email_address           = 'Your.Account@gmail.com', -- <-- change this
    15.     @display_name            = 'GmailDBMail',
    16.     @replyto_address         = 'Your.Account@gmail.com', -- <-- change this
    17.     @description             = '',
    18.     @mailserver_name         = 'smtp.gmail.com',
    19.     @mailserver_type         = 'SMTP',
    20.     @port                    = '587',
    21.     @username                = 'Your.Account@gmail.com', -- <-- change this
    22.     @password                = 'yourpassword', -- <-- change this
    23.     @use_default_credentials =  0 ,
    24.     @enable_ssl              =  1 ;
    25.   END --IF EXISTS  account
    26.  
    27. IF NOT EXISTS(SELECT *
    28.               FROM msdb.dbo.sysmail_profileaccount pa
    29.                 INNER JOIN msdb.dbo.sysmail_profile p ON pa.profile_id = p.profile_id
    30.                 INNER JOIN msdb.dbo.sysmail_account a ON pa.account_id = a.account_id  
    31.               WHERE p.name = 'GmailDBMail'
    32.                 AND a.name = 'GmailDBMail')
    33.   BEGIN
    34.    
    35.     EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
    36.       @profile_name = 'GmailDBMail',
    37.       @account_name = 'GmailDBMail',
    38.       @sequence_number = 1 ;
    39.   END

    If you like to use the wizard, you can do that as well, here is what it would look like

    Make sure to check SSL and that the port number is 587

    Now that it is all done, let's send a test email

    You might have to restart SQL Agent for Database Mail to start working, so do that first.

    Now execute the following stored proc, change your.account@gmail.com to where you want to send it

    1. EXEC msdb.dbo.sp_send_dbmail
    2.     @profile_name = 'GmailDBMail',
    3.     @recipients = 'your.account@gmail.com',
    4.     @body = 'The test finished successfully.',
    5.     @subject = 'Testing gmail with dbmail' ;

    Go and check that email inbox, did you get the email? If you did not get the email, make sure to check the Database Mail Log, it will contain messages telling you what the error is. Right click on Database Mail and select View Database Mail Log

    A message might be the following, this is because the port was wrong
    The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2013-01-13T08:04:38). Exception Message: Cannot send mails to mail server. (The operation has timed out.).

    That is all there is to setting up Database mail with gmail

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    4983 views
    InstapaperVote on HN

    4 comments

    Comment from: Ben [Visitor]
    Ben I had previously set up DB Mail with my personal Gmail account. It worked for a year or so, but then stopped a few months ago. I did not immediately notice it, but it turns out the cause was due to enabling two-factor authentication. For those that use this, see the following page for details on how to allow SQL Server to still authenticate:
    http://support.google.com/accounts/bin/answer.py?hl=en&answer=185833
    01/14/13 @ 20:07
    Comment from: Ben [Visitor]
    Ben Great article, very easy to understand. I did have to go in and enable the Database Mail XPs. It was easy I found the asnwer here:

    http://msdn.microsoft.com/en-us/library/ms191189.aspx

    Next, I executed the folowing:

    sp_configure 'show advanced options', 1;
    GO
    RECONFIGURE;
    GO
    sp_configure 'Database Mail XPs', 1;
    GO
    RECONFIGURE
    GO

    and it worked great!
    Thanks again
    01/19/13 @ 12:51
    Comment from: SQLDenis [Member] Email
    SQLDenis Ben, Correct database mail has to be enabled, SQL Server ships with pretty much everything turned off by default since version 2005
    01/19/13 @ 12:55
    Comment from: Manoj [Visitor] Email
    Manoj How to make the entire thing happen through SQL SERVER AGENT. So that in a particular date and time one email get triggered to the respective user.
    05/15/13 @ 20:52

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)