Solved

How Do I Embed an image using CDO within a SQL Server 2000 Stored Procedure

Posted on 2011-03-17
2
1,266 Views
Last Modified: 2012-05-11
I have created the following Stored Procedure in SQL Server 2000 in an attempt to embed an image into an HTML formatted email.  Though I get the email - the image fails to appear (I get the infamous red x where the image should appear.   I've tried referencing the image as a URL, a network file location and a direct path.  Below is my code - what is it I seem to be missing??

CREATE PROCEDURE dbo.ART_SENDMAIL
 (
  @FROM                       VARCHAR(200),
  @TO                         VARCHAR(200),
  @SUBJECT                    VARCHAR(200),
  @MSG_CONTENT                VARCHAR(8000),
  @MSG_TYPE                   VARCHAR(200),
  @MAIL_SERVER                VARCHAR(200)
 )
AS
 BEGIN
   DECLARE @iMsg          INT,
           @hr            INT,
           @source        VARCHAR(255),
           @description   VARCHAR(500),
           @output        VARCHAR(1000),    
           @BP            INT,
           @image         VARCHAR(100),
           @image_path    VARCHAR(2000)

   SET @image       = 'HR_Birthday.gif'
   SET @image_path  = 'http://artweb/images/'
   --SET @image_path  = '\\file01\Shares\DropBox\'
   --SET @image_path  = 'C:\ArtDorr\'
   SET @MSG_CONTENT = '<html><head><body><img src="cid:' + @image + '"><br><center><b>Testing....</b></center><body></html>'          

   EXEC @hr = sp_OACreate 'CDO.Message', @iMsg OUT
   EXEC @hr = sp_OASetProperty @iMsg,'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'
   EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', @MAIL_SERVER
   EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout").Value','10'
   EXEC @hr = sp_OAMethod @iMsg, 'Configuration.Fields.Update', null
   EXEC @hr = sp_OASetProperty @iMsg, 'MimeFormatted', '1'
   EXEC @hr = sp_OASetProperty @iMsg, 'To', @TO
   EXEC @hr = sp_OASetProperty @iMsg, 'From', @FROM
   EXEC @hr = sp_OASetProperty @iMsg, 'Subject', @SUBJECT

   -- If you are using HTML e-mail, use 'HTMLBody' instead of 'TextBody'.
   EXEC @hr = sp_OASetProperty @iMsg, @MSG_TYPE , @MSG_CONTENT
   
   EXEC @hr = sp_OAMethod @iMsg, 'AddRelatedBodyPart', @BP out, @image_path, @image, '1'
   EXEC @hr = sp_OASetProperty @BP, 'Fields.Item("urn:schemas:mailheader:Content-ID").Value', @image
   EXEC @hr = sp_OAMethod @BP, 'Fields.Update', null

   EXEC @hr = sp_OAMethod @iMsg, 'Send', NULL

   -- Error handling.
   IF @hr <>0
     BEGIN
       SELECT @hr
     END

   BEGIN
     EXEC @hr = sp_OAGetErrorInfo NULL, @source OUT, @description OUT
     IF @hr = 0
       BEGIN
         SELECT @output = ' Source: ' + @source
         PRINT @output
         SELECT @output = ' Description: ' + @description
         PRINT @output
       END
     ELSE
       BEGIN
         PRINT ' sp_OAGetErrorInfo failed.'
         RETURN
       END
   END

   -- Clean up the objects created.
   EXEC @hr = sp_OADestroy @iMsg


 END
0
Comment
Question by:artdorr
2 Comments
 
LVL 19

Accepted Solution

by:
nschafer earned 500 total points
ID: 35169114
You are very close.  The @Image_Path variable must contain the path and the file name.

See the attached.

Hope the helps,

Neal.
ALTER PROCEDURE dbo.ART_SENDMAIL
 (
  @FROM                       VARCHAR(200),
  @TO                         VARCHAR(200),
  @SUBJECT                    VARCHAR(200),
  @MSG_CONTENT                VARCHAR(8000),
  @MSG_TYPE                   VARCHAR(200),
  @MAIL_SERVER                VARCHAR(200)
 )
AS
 BEGIN 
   DECLARE @iMsg          INT,
           @hr            INT,
           @source        VARCHAR(255),
           @description   VARCHAR(500),
           @output        VARCHAR(1000),    
           @BP            INT,
           @image         VARCHAR(100),
           @image_path    VARCHAR(2000)

   SET @image       = 'logo1.jpg'  -- changed to a file I have
   --SET @image_path  = 'http://artweb/images/'
   --SET @image_path  = '\\file01\Shares\DropBox\'
   SET @image_path  = 'C:\logo1.jpg'  -- must contain path and file name
   SET @MSG_CONTENT = '<html><head><body><img src="cid:' + @image + '"><br><center><b>Testing....</b></center><body></html>'           

   EXEC @hr = sp_OACreate 'CDO.Message', @iMsg OUT
   EXEC @hr = sp_OASetProperty @iMsg,'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'
   EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', @MAIL_SERVER 
   EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout").Value','10'
   EXEC @hr = sp_OAMethod @iMsg, 'Configuration.Fields.Update', null
   EXEC @hr = sp_OASetProperty @iMsg, 'MimeFormatted', '1'
   EXEC @hr = sp_OASetProperty @iMsg, 'To', @TO
   EXEC @hr = sp_OASetProperty @iMsg, 'From', @FROM
   EXEC @hr = sp_OASetProperty @iMsg, 'Subject', @SUBJECT

   -- If you are using HTML e-mail, use 'HTMLBody' instead of 'TextBody'.
   EXEC @hr = sp_OASetProperty @iMsg, @MSG_TYPE , @MSG_CONTENT
   
   EXEC @hr = sp_OAMethod @iMsg, 'AddRelatedBodyPart', @BP out, @image_path, @image, '1'
   EXEC @hr = sp_OASetProperty @BP, 'Fields.Item("urn:schemas:mailheader:Content-ID").Value', @image
   EXEC @hr = sp_OAMethod @BP, 'Fields.Update', null 

   EXEC @hr = sp_OAMethod @iMsg, 'Send', NULL

   -- Error handling.
   IF @hr <>0 
     BEGIN
       SELECT @hr
     END

   BEGIN
     EXEC @hr = sp_OAGetErrorInfo NULL, @source OUT, @description OUT
     IF @hr = 0
       BEGIN
         SELECT @output = ' Source: ' + @source
         PRINT @output
         SELECT @output = ' Description: ' + @description
         PRINT @output
       END
     ELSE
       BEGIN
         PRINT ' sp_OAGetErrorInfo failed.'
         RETURN
       END
   END

   -- Clean up the objects created.
   EXEC @hr = sp_OADestroy @iMsg


 END

Open in new window

0
 

Author Closing Comment

by:artdorr
ID: 35169331
Excellent!  It's always the small thing isn't it!

Thanks for the help!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query Optimization 14 42
sql 2014,  lock limit 5 29
Simple SQL query from two tables 13 51
SQL View nearest date 5 35
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

785 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