SQL Server - Embed image in html and send html as email

Asked By Naresh Kumar on 24-Nov-11 05:22 AM
Can any one knows how to embed image in html and send html as email by msdb.dbo.sp_send_dbmail .

Please advise.
Jitendra Faye replied to Naresh Kumar on 24-Nov-11 05:25 AM
Try like this-

declare @body1 varchar(4000)
set @body1 = '<head>
<title> Embedded Logo Example</title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<table><tr><td valign="top" align="left">MyHeader</td></tr>
<tr><td valign="top" align="left"><img src="cid:sqlservercentral_logo.gif" width="235" height="70" border="0" alt="">
</td></tr>
</table>
</body>'





EXEC msdb.dbo.sp_send_dbmail
    @profile_name
='MyDefault EmailProfile',

    @recipients
='lowell@somedomain.net',
    @subject
= 'SQl 2008 email test',
    @body
= @body1,
    @body_format
= 'HTML',
    @query
= 'SELECT top 3 * from sysobjects where xtype=''U''',
    @query_result_header
= 0,
    @exclude_query_output
= 1,
    @append_query_error
= 1,
    @attach_query_result_as_file
= 1,
    @query_attachment_filename
= 'results.txt',
    @query_result_no_padding
= 1,
    @file_attachments
= 'C:\sqlservercentral_logo.gif'
Deepak More replied to Jitendra Faye on 11-Nov-13 10:37 AM
I tried this trick. but It didnt work for me.

it actually show a file attachment. image is not loading in img tag using cid. 
when i inspect that elemnt there i found that src attribute is getting lost.