Solved

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

Posted on 2011-03-17
2
1,250 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
Comment Utility
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
Comment Utility
Excellent!  It's always the small thing isn't it!

Thanks for the help!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now