Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 ASP Email and SP

Author  Topic 

budgie
Starting Member

18 Posts

Posted - 2004-07-15 : 06:26:57
With the help of http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=36293 I have written the following SP:

ALTER PROCEDURE usp_sql2email (
@mx NVARCHAR(255) = 'mailserver',
@from NVARCHAR(320) = 'example@example.com',
@from_name NVARCHAR(320) = 'Company Name',
@to NVARCHAR(320),
@to_name NVARCHAR(320),
@cc NVARCHAR(320) = NULL,
@cc_name NVARCHAR(320) = NULL,
@bcc NVARCHAR(320) = NULL,
@bcc_name NVARCHAR(320) = NULL,
@subject NVARCHAR(255),
@body NVARCHAR(4000),
@file_name NVARCHAR(255) = NULL,
@file_cont NVARCHAR(4000) = NULL
)
AS
DECLARE @int_result INT
DECLARE @int_object INT
DECLARE @str_source NVARCHAR(255)
DECLARE @str_desc NVARCHAR(500)

-- Create Object
EXECUTE @int_result = sp_OACreate 'Persits.MailSender', @int_object OUT
IF @int_result <> 0 GOTO CLEANUP
-- Set Propertiest & Methods
EXECUTE @int_result = sp_OASetProperty @int_object, 'host', @mx
IF @int_result <> 0 GOTO CLEANUP
EXECUTE @int_result = sp_OASetProperty @int_object, 'from', @from
IF @int_result <> 0 GOTO CLEANUP
EXECUTE @int_result = sp_OASetProperty @int_object, 'fromName', @from_name
IF @int_result <> 0 GOTO CLEANUP
EXECUTE @int_result = sp_OAMethod @int_object, 'addAddress', NULL, @to, @to_name
IF @int_result <> 0 GOTO CLEANUP
IF @cc IS NOT NULL
BEGIN
IF @cc_name IS NOT NULL
BEGIN
EXECUTE @int_result = sp_OAMethod @int_object, 'addCC', NULL, @cc, @cc_name
IF @int_result <> 0 GOTO CLEANUP
END
ELSE
BEGIN
EXECUTE @int_result = sp_OAMethod @int_object, 'addCC', NULL, @cc
IF @int_result <> 0 GOTO CLEANUP
END
END
IF @bcc IS NOT NULL
BEGIN
IF @bcc_name IS NOT NULL
BEGIN
EXECUTE @int_result = sp_OAMethod @int_object, 'addBCC', NULL, @bcc, @bcc_name
IF @int_result <> 0 GOTO CLEANUP
END
ELSE
BEGIN
EXECUTE @int_result = sp_OAMethod @int_object, 'addBCC', NULL, @bcc
IF @int_result <> 0 GOTO CLEANUP
END
END
EXECUTE @int_result = sp_OASetProperty @int_object, 'subject', @subject
EXECUTE @int_result = sp_OASetProperty @int_object, 'body', @body
IF @file_name IS NOT NULL
BEGIN
IF @file_cont IS NOT NULL
BEGIN
EXECUTE @int_result = sp_OAMethod @int_object, 'AddAttachment', NULL, @file_name
IF @int_result <> 0 GOTO CLEANUP
END
ELSE
BEGIN
EXECUTE @int_result = sp_OAMethod @int_object, 'AddMenAttachment', NULL, @file_name, @file_cont
IF @int_result <> 0 GOTO CLEANUP
END
END
-- Send Email (returns a recordset)
EXECUTE @int_result = sp_OAMethod @int_object, 'send', NULL
GOTO CLEANUP

CLEANUP:
-- Check whether an error occurred.
IF @int_result <> 0
BEGIN
-- Report the error.
EXEC sp_OAGetErrorInfo @int_object, @str_source OUT, @str_desc OUT
SELECT int_result=convert(varbinary(4),@int_result), Source=@str_source, Description=@str_desc
END

-- Destroy the object.
EXEC @int_result = sp_OADestroy @int_object
-- Check if an error occurred.
IF @int_result <> 0
BEGIN
-- Report the error.
EXEC sp_OAGetErrorInfo @int_object, @str_source OUT, @str_desc OUT
SELECT int_result=convert(varbinary(4),@int_result), Source=@str_source, Description=@str_desc
END
RETURN


I have been testing it in QA but can't understand why I get a returned recorded set from the send command. All the recordset contains is 1 column named column1 (surprise, surprise) with a value of 1. Surely it shouldn't return anything and wont this sproc break the calling system due to the fact that it could return 2 recordset when there is an error from the object (N.B. I have had this happen to me before between a sproc and the calling asp page - I am an ASP developer mainly but do most of the SQL dev work too)

Thanks in advance for the input,

G.

Kristen
Test

22859 Posts

Posted - 2004-07-15 : 09:06:17
You probably need a SET NOCOUNT ON immediately after the "AS"?

Kristen
Go to Top of Page

budgie
Starting Member

18 Posts

Posted - 2004-07-15 : 09:34:09
quote:
Originally posted by Kristen

You probably need a SET NOCOUNT ON immediately after the "AS"?



I have tried with and without SET NOCOUNT ON and still have the same result.

G.
Go to Top of Page

vsrajan76
Starting Member

10 Posts

Posted - 2004-07-15 : 11:30:53
try 'SendToQueue'.. instead of 'send' in this line...
sp_OAMethod @int_object, 'send', NULL

Hope this help!!!
-Sundar
Go to Top of Page

budgie
Starting Member

18 Posts

Posted - 2004-07-16 : 12:37:42
quote:
Originally posted by vsrajan76

try 'SendToQueue'.. instead of 'send' in this line...
sp_OAMethod @int_object, 'send', NULL


Again this isn't an option as it requires a fully registered version of ASPEmail. This company is too skin-flint to fork out for it.

Anyway I have come across another issue that has just blown this idea out of the water so we wont be using it for this. However it would still be interesting to find out what the issue is and how to resolve if I ever come across the situation again where this would be a viable option.

G
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-07-17 : 03:20:52
Make a wrapper SProc for usp_sql2email that INSERTS the output into some #TEMP table and basically throws them away?

Kristen
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-07-17 : 09:25:46
What blew it out of the water?

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

budgie
Starting Member

18 Posts

Posted - 2004-07-17 : 12:22:31
quote:
Originally posted by derrickleggett

What blew it out of the water?



The host doesn't allow use of extended procedures :(

G.
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-07-17 : 12:46:44
Well, geeee, that's a problem. Tell them you're going to use someone else if they don't grant you EXEC on the sp_OAmethod procs. They'll come around. There's too much competition out there.

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

budgie
Starting Member

18 Posts

Posted - 2004-07-17 : 14:19:12
quote:
Originally posted by derrickleggett

Well, geeee, that's a problem. Tell them you're going to use someone else if they don't grant you EXEC on the sp_OAmethod procs. They'll come around. There's too much competition out there.



I would except it is an internal department on this project and they are security crazy even though they have a massive firewall protecting the machines.
Go to Top of Page
   

- Advertisement -