Wednesday, 10 September 2025

Send email in SQL Server using Store Procedure

Introduction: Hello Guys, “Welcome back” Today, i am here with another one new great article. In this article I explained the implementation about how to send email in SQL Server using Store procedure.

Join our Channel  on Whtsapp and Telagram for All Updates                                          


Implementation: This article will explain that how to send email using sp_send_dbmail Store Procedure in SQL Server by using GMAIL SMTP settings.

This article is applicable to following SQL Server versions i.e. 2008, 2008R2, 2012, 2014, 2016, 2017, 2019 and 2022.

Configuring SQL Server for sending emails

In order to start sending emails, the very first thing to be done is to configure SQL Server for sending emails from Database. I already explained about to configuration of SQL Server in my article.

Sending Email using sp_send_dbmail Stored Procedure in SQL Server

Once the SQL Server Database is configured, you are now ready to send emails.

In the following SQL query, the SQL Server using Store Procedure sp_send_dbmail is used to send an email from SQL Server.

EXEC msdb.dbo.sp_send_dbmail

    @profile_name = 'SanjeevKumar_Email_Profile'

   ,@recipients = 'receiver@gmail.com'

   ,@subject = 'Email with SQL Server'

   ,@body = 'Write here complete detailed message:)'

   ,@importance ='HIGH'

GO

 Once executed, the following messages will appear which confirms, your email has been queued.

Checking Emails Status and Errors using Log Tables

Once the Store Procedure sp_send_dbmail will be executed, the mails will be queued and then sent to desired email address.

In case if you will not receive email, you can find the status of sending email i.e. sent, unsent or failed.

GMAIL SMTP Errors

The possible errors (exceptions) occurring while sending email using GMAIL SMTP will also be  covered in my next articles.

Conclusion: In above code, I have been explained implementation about how to send email in SQL Server using Store procedure. This code is very helpful for every developer. Bye Bye and take care of yourself Developers. We will come back shortly with the new article.

Regards

Using Asp.net

 

No comments:

Post a Comment