Introduction
A very interesting topic of discussion. We have mail integrated to every application nowadays. We integrate email using SMTP settings in the Web.Config in .NET and use the Send method to send emails. Recently, I came across an interesting challenge, where we were to send emails from our SQL Server. Suppose we have to track the successful scheduled sql query execution. We cannot look into the tables it modified every time in order to check if it actually ran through successfully. It would be so nice, if we could get some kind of notification which can help us know about the status of execution. Yes, it is possible to send mails from our SQL server using few stored procedures which are actually pre-defined. Lets learn how: 
Get Started
Remember we will be using predefined Stored procedure to send the mails. Firstly, we need to set up an account with the credentials required by the server to send the emails. Usually the mail is sent through SMTP, Simple Mail Transfer Protocol. The settings would depend on the server your application demands. Remember the configuration needs to be valid. Create a Database Account:
- EXEC msdb.dbo.sysmail_add_account_sp
- @account_name = 'SendEmailSqlDemoAccount'
- , @description = 'Sending SMTP mails to users'
- , @email_address = '[email protected]'
- , @display_name = 'Suraj Sahoo'
- , @replyto_address = '[email protected]'
- , @mailserver_name = 'smtp.gmail.com'
- , @port = 587
- , @username = 'XXXXXX'
- , @password = 'XXXXXX'
- Go
- EXEC msdb.dbo.sysmail_add_profile_sp
- @profile_name = 'SendEmailSqlDemoProfile'
- , @description = 'Mail Profile description'
- Go
- -- Add the account to the profile
- EXEC msdb.dbo.sysmail_add_profileaccount_sp
- @profile_name = 'SendEmailSqlDemo'
- , @account_name = 'SendEmailSql'
- , @sequence_number = 1
- GO
- EXEC msdb.dbo.sp_send_dbmail
- @profile_name = 'SendEmailSqlDemo2'
- , @recipients = '[email protected]'
- , @subject = 'Automated Test Results (Successful)'
- , @body = 'The stored procedure finished successfully.'
- , @importance ='HIGH'
- GO
- BEGIN TRY
- BEGIN TRAN
- INSERT INTO
- dbo.[User]
- SELECT
- us.UserName,
- us.UserAddress,
- us.UserPhone,
- @fkScreenID
- FROM
- dbo.[User] as us
- WHERE
- UserID= @userID
- COMMIT TRAN
- END TRY
- BEGIN CATCH
- ROLLBACK TRAN
- END
- END CATCH //Similarly for other tables as well we continue. Its is better to add the Try Catch to whole SP Executing Block
Troubleshooting Mails
There are also stored procedure to let us know if the mails are successful, failed or remained in the queue. This is a fascinating feature. To check for the mails which were successfully sent and delivered, we run the following query:
- select * from msdb.dbo.sysmail_sentitems

In the second image you can see we have the sent_status as sent, which states the mail has been successfully sent. To check for the unsent mails which could not be sent, we run the following query:
- select * from msdb.dbo.sysmail_unsentitems
- select * from msdb.dbo.sysmail_faileditems
- SELECT items.subject,
- items.last_mod_date
- ,l.description FROM msdb.dbo.sysmail_faileditems as items
- INNER JOIN msdb.dbo.sysmail_event_log AS l
- ON items.mailitem_id = l.mailitem_id
- GO
The error description above is like "No Such Host" Error. This error usually comes when we have some SMTP server connection settings wrong. We need to troubleshoot that on our own and recheck the settings credentials and then try. If then it does not seem to work, we need to look for the DNS server settings and retry with the configuration again. Nothing to worry for this though.
Conclusion
Thus we discussed here about sending mails from our own SQL using the stored procedures and how helpful they can prove to be. Troubleshooting the errors is very easy here. Exceptions and errors are a part of development which cannot be avoided but handling them is a challenge and developers can easily do that.
References

Ankur MistryPosted Nov 29, 2015, 9:26 AM
Nice
Humayun Kabir MamunPosted Sep 11, 2015, 2:29 AM
Nice...
Ricky NguyenPosted Sep 10, 2015, 12:28 PM
Thanks for sharing :D
Prem Anandh JPosted Sep 9, 2015, 7:08 AM
Thanks for sharing..
Ankit BansalPosted Sep 9, 2015, 4:30 AM
thanks for sharing..
Amol SarkatePosted Sep 9, 2015, 3:07 AM
thanks for giving this valuable information sir.....!
Yashwant VishwakarmaPosted Sep 9, 2015, 1:03 AM
Very Informative !!
Rajeesh MenothPosted Sep 9, 2015, 12:54 AM
Good One Sir
Praveen KumarPosted Sep 8, 2015, 11:53 PM
very nice Suraj Sahoo thanks for sharing
Jaipal ReddyPosted Sep 8, 2015, 11:29 PM
Nice article sir.
Karthikeyan KPosted Sep 8, 2015, 11:22 PM
Good one sir...Thanks for sharing
Pankaj Kumar ChoudharyPosted Sep 8, 2015, 7:56 PM
Nice Article Sir...........
Mahesh ChandPosted Sep 8, 2015, 6:05 PM
Nice Suraj and thank you for following code formatting.
Santhakumar MunuswamyPosted Sep 8, 2015, 3:14 PM
Good One. Thanks for sharing
Mohammed IbrahimPosted Sep 8, 2015, 3:12 PM
nice share
Vignesh ManiPosted Sep 8, 2015, 1:33 PM
Good one
Manas MohapatraPosted Sep 8, 2015, 1:18 PM
Very Informative SIR..