In this article I will explain with an example, how to send html formatted emails in
SQL Server.
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.
Sending HTML formatted emails in SQL Server
Once the
SQL Server Database is configured for sending emails, you are now ready to sending html formatted emails.
In the following SQL query, the Stored Procedure sp_send_dbmail is used to send an email from SQL Server.
DECLARE @messageBody NVARCHAR(1000) = '<b>Happy Birthday Mudassar Khan</b>.<br />'
+ 'Many happy returns of the day.'
+ '<br /><br />ASPSnippets Team'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'mudassar_Email_Profile'
,@recipients = 'recipient@gmail.com'
,@subject = 'Email from SQL Server'
,@body = @messageBody
,@body_format = 'HTML'
,@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
Stored Procedure sp_send_dbmail is executed, the mails are queued and then sent.
GMAIL SMTP Errors
The possible errors (exceptions) occurring while sending email using GMAIL SMTP are covered in the following article.
Screenshot
Downloads