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

LessThanDot

All Blogs

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

    Tags: gmail

    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
    5352 views
    InstapaperVote on HN