Solved

sp_send_dbmail format

Posted on 2011-02-16
6
1,130 Views
Last Modified: 2012-06-21
when sending an email through the above proc in SQL Server to outlook with HTML format, it is jumbled and not proper. what does outlook need for it to receive the format in nice looking way?

thanks
0
Comment
Question by:anushahanna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 34911376
You need to format the document like in the example below:

DECLARE @title nvarchar(200)
DECLARE @tableHTML nvarchar(MAX)

SET @title = 'Summary Report - Passive and Active List'
SET @tableHTML =
    N'<H1>' + @title + '</H1>' +
    N'<table border="1">' +
    N'<tr>' +
      N'<th>gway</th>' +
      N'<th>valid_count</th>' +
      N'<th>active</th>' +
      N'<th>excluded</th>' +
   CAST(
            (SELECT
                  td = gway, '',
                  td = flag, '',
                  td = case when active = 1 then 'YES' else 'NO' END ,'',
                  td = case when gel = 1 then 'YES' else 'NO' END ,''
            FROM  servername.dbo.tablename
            FOR XML PATH('tr'), TYPE)
      AS NVARCHAR(MAX) ) + N'</table>' ;

IF @tableHTML is not null
BEGIN
    EXEC msdb.dbo.sp_send_dbmail
            @profile_name = 'your_profile',
            @recipients='anushahanna@mail.com',
            @subject = @title,
            @body = @tableHTML ,
            @body_format = 'HTML' ;
END
0
 
LVL 6

Author Comment

by:anushahanna
ID: 34935836
thanks lcohan- does this make sure any recipient mails, will receive it ok, in proper format (as long the recipient mail system supports HTML)?
0
 
LVL 40

Accepted Solution

by:
lcohan earned 500 total points
ID: 34954808
That is correct including even blackberry users on both 2005 and 2008 so far.
Please test is and adjust it to your table structure - other nice thing is that if table is empty like you have nothing to send that particular time they won't get an email with nothing attached.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 6

Author Comment

by:anushahanna
ID: 34979810
lcohan- thanks.

can you please add some dummy values to the below code, just to prove the point of how pretty this can be made. can we add 2 rows of data for the 4 columns and put some colors?
DECLARE @title nvarchar(200)
DECLARE @tableHTML nvarchar(MAX)

SET @title = 'Summary Report - FunctionCode'
SET @tableHTML =
    N'<H1>' + @title + '</H1>' +
    N'<table border="1">' +
    N'<tr>' +
      N'<th>Column1</th>' +
      N'<th>Column2</th>' +
      N'<th>Column3</th>' +
      N'<th>Column4</th>'  + N'</table>' ;

IF @tableHTML is not null
BEGIN
    EXEC msdb.dbo.sp_send_dbmail
            @profile_name = 'ProfileName',
            @recipients='anushahanna@mail.com',
            @subject = @title,
            @body = @tableHTML ,
            @body_format = 'HTML' ;
END

Open in new window

0
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 500 total points
ID: 34981164
I'm pretty sure ther must be a way to color it and you should be able to find that in SQL BOL however to get the example you wanted just replace your mail profile and actual email in query below:

create table #test (column1 int, column2 text, column3 text, column4 text)
insert into #test SELECT 1 ,'text2','text3','text4'
insert into #test SELECT 2 ,'code2','code3','code4'


DECLARE @title nvarchar(200)
DECLARE @tableHTML nvarchar(MAX)

SET @title = 'Summary Report - FunctionCode'
SET @tableHTML =
    N'<H1>' + @title + '</H1>' +
    N'<table border="1">' +
    N'<tr>' +
      N'<th>Column1</th>' +
      N'<th>Column2</th>' +
      N'<th>Column3</th>' +
      N'<th>Column4</th>' + 
     CAST(
            (SELECT
                  td = Column1 ,'',
                  td = Column2,'',
                  td = Column3,'',
                  td = Column4,''
            FROM  #test WHERE column1 in (1,2)
            FOR XML PATH('tr'), TYPE) 
	 AS NVARCHAR(MAX) ) 
	 + N'</table>' ;

IF @tableHTML is not null
BEGIN
    EXEC msdb.dbo.sp_send_dbmail
            @profile_name = 'mail_profile',
            @recipients='user@mail.com',
            @subject = @title,
            @body = @tableHTML ,
            @body_format = 'HTML' ;
END

drop table #test

Open in new window

0
 
LVL 6

Author Comment

by:anushahanna
ID: 34982861
very nice, lcohan - thanks much.
0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question