Click here to Skip to main content
Click here to Skip to main content

SQL SERVER - 2008 - Configure Database Mail - Send Email From SQL Database

By , 3 Sep 2008
 

Introduction

In this article, I discuss about the Database Mail which is used to send the email using SQL Server. Previously I had discussed about SQL SERVER - Difference Between Database Mail and SQLMail. Database mail is the replacement for SQLMail with many enhancements. So one should stop using SQLMail and upgrade to the Database Mail.

In order to send mail using Database Mail in SQL Server, there are 3 basic steps that need to be carried out:

  1. Create Profile and Account
  2. Configure Email
  3. Send Email

Step 1: Create Profile and Account

You need to create a profile and account using the Configure Database Mail Wizard which can be accessed from the Configure Database Mail context menu of the Database Mail node in Management Node.

This wizard is used to manage accounts, profiles, and Database Mail global settings which are shown below:

Step 2: Configure Email

After the Account and the Profile are created successfully, we need to configure the Database Mail. To configure it, we need to enable the Database Mail XPs parameter through the sp_configure stored procedure, as shown here:

sp_CONFIGURE 'show advanced', 1 GO RECONFIGURE GO sp_CONFIGURE _
	'Database Mail XPs', 1 GO RECONFIGURE GO 

Step 3: Send Email

After all configurations are done, we are now ready to send an email. To send mail, we need to execute a stored procedure sp_send_dbmail and provide the required parameters as shown below:

USE msdb GO EXEC sp_send_dbmail @profile_name='PinalProfile', _
@recipients='test@Example.com', @subject='Test message', _
@body='This is the body of the test message. _
Congrats Database Mail Received By you Successfully.' 

After all validations of the parameters entered are done, certain stored procedures are executed and the mail is queued by Service Broker. Read more at SQL SERVER - Introduction to Service Broker.

Database Mail keeps copies of outgoing e-mail messages and displays them in the sysmail_allitems, sysmail_sentitems, sysmail_unsentitems, sysmail_faileditems. The status of the mail sent can be seen in sysmail_mailitems table. When the mail is sent successfully, the sent_status field of the sysmail_mailitems table is set to 1 which can again be seen in sysmail_sentitems table. The mails that have failed will have the sent_status field value to 2 and those are unsent will have value 3. The log can be checked in sysmail_log table as shown below:

SELECT * FROM sysmail_mailitems GO SELECT * FROM sysmail_log GO 

Status can be verified using sysmail_sentitems table.

After sending mail, you can check the mail received in your inbox, just as I received as shown below:

Let me know what you think about this tutorial.

Reference: Pinal Dave (http://www.SQLAuthority.com)

History

  • 3rd September, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

pinaldave
Founder http://blog.SQLAuthority.com
India India
Member
Pinal Dave is a Microsoft Technology Evangelist (Database and BI). He has written over 2200 articles on the subject on his blog at http://blog.sqlauthority.com. Along with 8+ years of hands on experience he holds a Masters of Science degree and a number of certifications, including MCTS, MCDBA and MCAD (.NET). He is co-author of two SQL Server books - SQL Server Programming, SQL Wait Stats and SQL Server Interview Questions and Answers. Prior to joining Microsoft he was awarded Microsoft MVP award for three continuous years for his contribution in community.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionMails Sending from SQL receiving in SPAM/JunkmemberMember 100241352 May '13 - 1:16 
Hi,
by looking at the screens, i am easily configured the email. But when SQL sends mail, i am receiving in SPAM/JUNK folder. Is there any resolution for this ???
 
Thanks
GeneralMy vote of 5memberMember 37463078 Apr '13 - 23:46 
great
GeneralMy vote of 5memberLokesh Zende19 Dec '12 - 2:10 
superb !!
GeneralMy vote of 5memberkishormahale28 Nov '12 - 18:42 
best one
GeneralMy vote of 5memberAmol_B4 Jun '12 - 19:57 
Clear and well documented
Thanks!
GeneralMy vote of 5memberP.Salini25 May '12 - 1:44 
Nice Useful to me
QuestionDatabase Mail Sending Failed From Sql Server 2008 R2 . Error Like Could not connect to mail server. (An attempt was made to access a socket in a way forbidden by its access permissionsmemberGV Sharma3 May '12 - 1:10 
Hi pinaldave.
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). )
Please suggest me how i can do it.
 
Thanks and Regards:
Ganpat Sharma
QuestionDatabase Mail Sending Failed From Sql Server 2008 R2 . Error Like Could not connect to mail server. (An attempt was made to access a socket in a way forbidden by its access permissions ?memberGV Sharma3 May '12 - 1:09 
Hi pinaldave.
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). )
Please suggest me how i can do it.
 
Thanks and Regards:
Ganpat Sharma
AnswerRe: Database Mail Sending Failed From Sql Server 2008 R2 . Error Like Could not connect to mail server. (An attempt was made to access a socket in a way forbidden by its access permissions ?memberKanwal Shehzad31 Jul '12 - 2:26 
Dear Ganpat,
 
In case of GMail, you should check following two things:
 
1) Port 587 is not blocked.
 
2) While configuring email in SQL Server, in the end of section [Outgoing mail server (SMTP)] there is one checkbox [This server requires a secure connection (SSL)], you should check this checkbox.
 
I hope your problem will be solved.
Kanwal Shehzad

GeneralMy vote of 5membergemasoft11 Mar '11 - 7:45 
That's what I call a well documented entry

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 3 Sep 2008
Article Copyright 2008 by pinaldave
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid