title: SQL Server Database Mail is sending blank messages when I give it an HTML body
body: <p>I'm trying to use the SQL Server Management Studio Database Mail to send automated HTML-formatted emails. It works fine when I give it a plain-text body, but as soon as I change it to HTML, the messages are blank. They still send with the proper subject and everything else, but the body isn't there.</p>
  
  <p>Here's the relevant bit of code:</p>
  
  <pre><code> DECLARE @msg varchar(8000); --body of the message in html.
   DECLARE @recipient_emails varchar(50); --List of email addresses for everyone who needs to receive this message.
   DECLARE @subject_line varchar(100); --Subject line for the email.
   DECLARE @num_of_msgs int; --Since some updates need to trigger multiple different emails, this is used to determine how many emails will be sent.
   DECLARE @creator varchar(10); --Creator of the opportunity in CRM.
   DECLARE @account_id int; --Account number for customer.
   DECLARE @customer varchar(50); --Customer from whom the opportunity came.
   DECLARE @opty_type varchar(30); --Type of opportunity.
   DECLARE @opty_desc varchar(40); --Description of opportunity.
   DECLARE @date_created datetime; --Datetime when opportunity was created.
   DECLARE @notes varchar(500); --Notes about opportunity.
  
   SELECT
   @creator = CREATOR,
   @account_id = ACCT_ID,
   @opty_type = OPTY_TYPE_ID,
   @opty_desc = DESCRIPTION,
   @date_created = OPEN_DATE,
   @notes = COMMENTS
   FROM V_OPPORTUNITY
   WHERE ID = @opty_id;
  
   --Uses a CASE statement to determine recipient emails based on what activity has just been completed.
   SET @recipient_emails =
   CASE @description
    WHEN 'Create Opportunity in CRM' THEN 'nate.lovell@aggiemail.usu.edu'
    WHEN 'Request NDA' THEN 'Email 2'
    WHEN 'Receive Formula' THEN 'Email 3'
    WHEN 'Receive Product Specs-general lbl claim &amp; pkg spec' THEN 'Email 4'
    WHEN 'Issue Ballpark Quote' THEN 'Email 5'
    WHEN 'Credit Check' THEN 'Email 6'
    ELSE ''
   END;
  
   --Uses a CASE statement to determine HTML message based on what activity has just been completed.
   SET @msg = 
   CASE @description
    WHEN 'Create Opportunity in CRM' THEN '&lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;DLI NEW CRM OPTY EMAIL&lt;/title&gt;
  &lt;style type="text/css"&gt;
  body {
      margin-left: 10px;
      margin-top: 10px;
      margin-right: 10px;
      margin-bottom: 10px;
      text-align: center;
      background-color: #61FF51;
  }
  div#PAGECENTER {
     width: 1000px;
      margin-left: auto;
     margin-right: auto
   }
  
  &lt;/style&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;table width="100%" border="2" cellspacing="5" cellpadding="5"&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td colspan="4" align="center" valign="middle" style="font-family: Consolas, ''Andale Mono'', ''Lucida Console'', ''Lucida Sans Typewriter'', Monaco, ''Courier New'', monospace; font-weight: bold; font-size: 36px;"&gt;&lt;p&gt;&lt;!--&lt;img src="/Images/DESLABLGtag3d.png" width="52" height="53" alt=""/&gt;--&gt;&lt;span style="font-size: 24px"&gt; NEW OPTY CREATED IN CRM&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td width="15%" height="40" align="right" valign="middle" style="font-weight: bold"&gt;CREATED BY&lt;/td&gt;
        &lt;td width="35%" height="40" align="left" valign="middle" bgcolor="#E5E5E5" style="font-size: 16px; font-family: Consolas, ''Andale Mono'', ''Lucida Console'', ''Lucida Sans Typewriter'', Monaco, ''Courier New'', monospace;"&gt;'+@creator+'&lt;/td&gt;
        &lt;td width="15%" height="40" align="right" valign="middle" style="font-weight: bold"&gt;CUSTOMER&lt;/td&gt;
        &lt;td width="35%" height="40" align="left" valign="middle" bgcolor="#E5E5E5" style="font-size: 16px; font-family: Consolas, ''Andale Mono'', ''Lucida Console'', ''Lucida Sans Typewriter'', Monaco, ''Courier New'', monospace;"&gt;SQL HERE&lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
        &lt;td width="15%" height="40" align="right" valign="middle" style="font-weight: bold"&gt;OPTY TYPE&lt;/td&gt;
        &lt;td width="35%" height="40" align="left" valign="middle" bgcolor="#E5E5E5" style="font-size: 16px; font-family: Consolas, ''Andale Mono'', ''Lucida Console'', ''Lucida Sans Typewriter'', Monaco, ''Courier New'', monospace;"&gt;'+@opty_type+'&lt;/td&gt;
        &lt;td width="15%" height="40" align="right" valign="middle" style="font-weight: bold"&gt;DESCRIPTION&lt;/td&gt;
        &lt;td width="35%" height="40" align="left" valign="middle" bgcolor="#E5E5E5" style="font-size: 16px; font-family: Consolas, ''Andale Mono'', ''Lucida Console'', ''Lucida Sans Typewriter'', Monaco, ''Courier New'', monospace;"&gt;'+@opty_desc+'&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td width="15%" height="40" align="right" valign="middle" style="font-weight: bold"&gt;DATE CREATED&lt;/td&gt;
        &lt;td width="35%" height="40" align="left" valign="middle" bgcolor="#E5E5E5" style="font-size: 16px; font-family: Consolas, ''Andale Mono'', ''Lucida Console'', ''Lucida Sans Typewriter'', Monaco, ''Courier New'', monospace;"&gt;'+CAST(@date_created AS varchar(20))+'&lt;/span&gt;&lt;/td&gt;
  
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td width="15%" height="40" align="right" valign="middle" style="font-weight: bold"&gt;NOTES&lt;/td&gt;
        &lt;td height="40" colspan="3" align="left" valign="middle" bgcolor="#E5E5E5" style="font-family: Consolas, ''Andale Mono'', ''Lucida Console'', ''Lucida Sans Typewriter'', Monaco, ''Courier New'', monospace"&gt;'+ISNULL(@notes,'')+'&lt;/span&gt;&lt;/td&gt;
      &lt;/tr&gt;
  
    &lt;/tbody&gt;
  &lt;/table&gt;
  &lt;div id="PAGECENTER"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;'
  
    WHEN 'Request NDA' THEN 'Msg 2'
    WHEN 'Receive Formula' THEN 'Msg 3'
    WHEN 'Receive Product Specs-general lbl claim &amp; pkg spec' THEN 'Msg 4'
    WHEN 'Issue Ballpark Quote' THEN 'Msg 5'
    WHEN 'Credit Check' THEN 'Msg 6'
    ELSE 'DEFAULT MSG'
   END;
  
   --Uses a CASE statement to determine subject line based on what activity has just been completed.
   SET @subject_line = 
   CASE @description
    WHEN 'Create Opportunity in CRM' THEN 'Subject 1'
    WHEN 'Request NDA' THEN 'Subject 2'
    WHEN 'Receive Formula' THEN 'Subject 3'
    WHEN 'Receive Product Specs-general lbl claim &amp; pkg spec' THEN 'Subject 4'
    WHEN 'Issue Ballpark Quote' THEN 'Subject 5'
    WHEN 'Credit Check' THEN 'Subject 6'
    ELSE 'DEFAULT SUBJECT'
   END;
  
   --Uses a CASE statement to determine number of messages based on what activity has just been completed.
   SET @num_of_msgs = 
   CASE @description
    WHEN 'Create Opportunity in CRM' THEN 2
    WHEN 'Request NDA' THEN 1
    WHEN 'Receive Formula' THEN 1
    WHEN 'Receive Product Specs-general lbl claim &amp; pkg spec' THEN 1
    WHEN 'Issue Ballpark Quote' THEN 1
    WHEN 'Credit Check' THEN 1
    ELSE 1
   END;
  
   --Sends an email.
   EXEC msdb.dbo.sp_send_dbmail @recipients = @recipient_emails, @body = @msg, @body_format = 'HTML',  @subject = @subject_line, @profile_name = 'SQLProfile';
  </code></pre>
  
  <p>NOTES: The only case I'm worried about getting to work right now is the one for 'Create Opportunity in CRM'. Also, several variables used here are declared and/or set earlier in the code, like @opty_id and @description. </p>