Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to send html emails using sp_send_dbmail

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2010-03-17 : 09:54:59
Is it possible to send html based emails using sp_send_dbmail component via sql server.

I am currently sending email notifications every 2 days using database mail component but the text inside the body of the email is plain without any html fonts table etc.

My users are requesting to change it to present html type mails instead of plain text, is it possible?

Thank you very much for the helpful info.

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-03-17 : 10:24:35
You need to do explicit formatting in your code.

Example:
DECLARE @xml NVARCHAR(MAX)
DECLARE @body NVARCHAR(MAX)
SET @xml =CAST(( SELECT ProductID AS 'td','',SUM(SalePrice) AS 'td'
FROM Sales GROUP BY ProductID FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))
SET @body ='<html><H1>Sales Reports</H1><body bgcolor=yellow>
<table border = 2><tr><th>Product ID </th><th>SaleAmount</th></tr>' SET @body = @body + @xml +'</table></body></html>'
EXEC msdb.dbo.sp_send_dbmail
@recipients =N'mymail@gmail.com',@body = @body,@body_format ='HTML',@subject ='Message Subject',@profile_name ='DatabaseMailProfile'


Regards,
Bohra
Go to Top of Page
   

- Advertisement -