Solved

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

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

895 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

21 Experts available now in Live!

Get 1:1 Help Now