Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

*** Stored Procedure - Using CDONTS to Send HTML Email Problem ***

Status
Not open for further replies.

jonnywah

Programmer
Feb 7, 2004
80
US
I am new to using sql server stored procedures and CDONTS to send HTML email. I am using "DECLARE @body VARCHAR(8000)", and the stored procedure compiles and sends the email.

When I look at the email in my mailbox, the email gets cut off around 15,000 characters (I have over 20,000 characters in my HTML code).

I tried using "DECLARE @body TEXT" instead of "DECLARE @body VARCHAR(8000)", but I get the error:

"The text, ntext, and image data types are invalid for local variables."

Please help. Any suggestions or information would be appreciated. Thank you in advance.



---------- MY CODE ---------------

CREATE PROCEDURE sp_SendNewsletter
AS

DECLARE @result INT
DECLARE @object INT

DECLARE @from VARCHAR(500)
DECLARE @to VARCHAR(500)
DECLARE @bcc VARCHAR(500)
DECLARE @subject VARCHAR(1000)
DECLARE @body VARCHAR(8000)
--The text, ntext, and image data types are invalid for local variables.
--DECLARE @body TEXT


BEGIN
Print 'Sending Test Newletter to 1 Email Address'


--Create the object, if @result <> 0, there is an error.
PRINT 'Creating the CDONTS.NewMail object'
EXEC @result = sp_OACreate 'CDONTS.NewMail', @object OUTPUT


IF @result <> 0
BEGIN
PRINT 'sp_OACreate Failed'
RETURN @result
END

--HTML email
EXEC @result = sp_OASetProperty @object, 'MailFormat',0
EXEC @result = sp_OASetProperty @object, 'BodyFormat',0

--Send Email using Send method, if @result <> 0, there is an error.
PRINT 'Sending the message using the Send method'

set @from = 'My Newsletter <newsletter@abc.com>'
set @to = 'test@abc.com'
set @subject = 'My Newsletter'

-- *************************** EMAIL CONTENT ***************************


--the store procedure compiles and works -> Problem is the HTML code
--in @body gets cut off (around 15,000 characters)
set @body = '<HTML><HEAD>Whole bunch of HTML here (over 20,000 characters)</HEAD><BODY>'



-- *************************** END OF EMAIL CONTENT *********************************

EXEC @result = sp_OAMethod @object, 'Send', NULL, @from, @to, @subject, @body, 0
IF @result <> 0
BEGIN
PRINT 'sp_OAMethod Failed'
RETURN @result
END

--Destroy the object, if @result <> 0, there is an error.
PRINT 'Destroying the CDONTS.NewMail object'
EXEC @result = sp_OADestroy @object
IF @result <> 0
BEGIN
PRINT 'sp_OADestroy Failed'
RETURN @result
END

Print 'Sending Test Newletter to 1 Email Address - DONE!'


END



GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO


 
Without seeing your HTML, I can only suggest you make the best use possible of CSS and other include files to render your HTML email.

Alternatively, you could make your newsletter an HTML email attachment, but if its HTML, why not place it on a server and just send a link and stop eating up your network bandwidth width ?
 
I once heard of someone who complained the computer asked them to insert the third disk into the floppy drive but they couldnt get it in - the first two were all that would fit.

I dont know how you got 15000 characters into a varchar(8000), so I dont rate the chances of getting 20000 in it. I would be spitting out a file and then attach it.
 
The problem wasn't really with the varchar (8000). Problem is that CDONTS sp_OASetProperty truncates data.

If you don't want to use an HTML attachment for HTML emails, you must use a different COM object for sending large HTML emails.

I got it working and I appreciate everyone's help. Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top