Solved

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

Posted on 2011-03-17
2
1,284 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
[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
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

688 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