SQL Database Mail — Send Emails in HTML Format


Usually the content of emails is sent by Database Mail in a plain text.  You can change it to HTML format for a richer content.  This article demonstrates how to do it.

The article here is assumed that a SQL Database Mail profile “MailProfile1′ has been created.  The MailProfile1 tells Database Mail what account it should use to send emails.  For how to create a Database Mail profile, you can refer to the previous post SQL Database Mail – Send Emails from SQL Server.

Here is the example to send emails in HTML format.

— Start T-SQL

    USE msdb
    EXEC sp_send_dbmail
      @profile_name = ‘MailProfile1’,
      @recipients = ‘someone@microsoft.com’,
      @subject = ‘HTML Format’,
      @body_format = ‘HTML’,
      @body = ‘<html><body><p>An html link to www.bing.com is embedded in this email. The link is here: <a href=”http://www.bing.com”>u bing?</a></p></body></html>’
     

— End T-SQL —

The recipient will receive an email like the following one.

From: Yuhong Li
Sent: Wednesday, January 05, 2011 12:09 PM
To: Some One
Subject: HTML Format

An html link to www.bing.com is embedded in this email. The link is here: u bing?

Click on the word “u bing?” in the email, it will redirect readers to http://www.bing.com.

For more information about sending emails, see sp_send_dbmail in MSDN documents.

<END OF POST>

 

Comments (2)

  1. rae says:

    is there a size limitation for the body of the html email?  There is with SQLMail

  2. LALIT says:

    Hi,

    The mail body gets formatted, but how to format the table in which output of query are appearing. They comes by default in Times new roman, and i want to change this to verdana.