TSQL - How To Send Email From SQL Server In HTML Format

We can send email from SQL Server in HTML format by using dbo.sp_send_dbmail stored procedure. To send email in HTML format, we have to build the body of email with HTML Tags. The below code can be used to send basis HTML formatted email. If you want to loop through some results of table and send in HTML formatted email. You can use This script for looping ( TSQL Cursor) and build the body by using below script.

--Get the Profile Name that you want to use 
--select * from msdb..sysmail_profile
DECLARE @Recipients VARCHAR(200)
SET @Recipients='aamirwarraich2001@gmail.com'
DECLARE @Subject VARCHAR(100)
SET @Subject='Test Email Subject'
DECLARE @Body NVARCHAR(2000)
DECLARE @ProfileName VARCHAR(100)
SET @ProfileName='EmailProfileName'
SET @Body=N'
<html>
 <head>
  <meta http-equiv=Content-Type content="text/html; charset=windows-1252">
  <style>p{font-size:10.0pt;font-family:Calibri,"Arial","serif";}</style>
 </head>
 <body>
 <p>Hello All,</p>
 <p>Sql Age is the blog spot to learn and share Information, Scenarios, Real Time Examples about SQL Server,<br />
  Transact-SQL (TSQL), SQL Server Database Administration (SQL DBA), Business Intelligence (BI),<br />
  SQL Server Integration Services (SSIS), SQL Server Reporting Services (SSRS) and Data Warehouse (DWH) Concepts.</p>
 <p>Thank you,<br />
  Development Team<br />
 <a href="http://sqlage.blogspot.com/"><b>sqlage.blogspot.com</b></a><br />
 </p>
 </body>
</html>'
EXEC msdb.dbo.sp_send_dbmail
  @profile_name =@ProfileName,
  @recipients = @Recipients,
  @subject = @Subject,
  @body = @Body,
  @body_format = 'HTML'


Here is the output of above script. If you want to change the fond size , format type that can be changed in above code as per requirement.
Fig 1: HTML Formatted Email send by SQL Server dbo.Sp_Send_dbMail Stored Procedure

6 comments:

  1. Replies
    1. IEEE Final Year Project centers make amazing deep learning final year projects ideas for final year students Final Year Projects for CSE to training and develop their deep learning experience and talents.

      IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation.

      corporate training in chennai corporate training in chennai

      corporate training companies in india corporate training companies in india

      corporate training companies in chennai corporate training companies in chennai

      I have read your blog its very attractive and impressive. I like it your blog. Digital Marketing Company in Chennai

      Delete
  2. Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also Site Tips Login

    ReplyDelete
  3. Hello fellas, I am back with yet another tutorial. In today’s tutorial I will show you how to make an account in hotmail and make a successful Hotmail Login. So let’s get started.

    ReplyDelete