|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-06-27 : 09:08:43
|
Joe writes "Hi GuysI've search high and low to try and resolve this particular problem. There seem to be a fair few articles out there that 'skirt' around the topic, but none that nail it down.I'm working on a stored procedure that sends mail using the CDONTS object installed by Microsoft SMTP service, but in HTML.I need to use CDONTS as the server is NT and SQLMail is not an option.I'm pretty sure that my stored procedure isn't conveying BodyFormat and MailFormat correctly.Could you have a look over my procedure, and put me out of my misery?SP:SET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOALTER PROCEDURE PrsendSMTPmail (@To varchar(8000),@Subject varchar(255),@Importance int = 2, @Cc varchar(8000) = NULL,@Bcc varchar(8000) = NULL,@Attachments varchar(8000) = NULL, @MailFormat int = 0,@BodyFormat int = 0,@From varchar(255) = NULL)ASSET NOCOUNT ONDECLARE @object intDECLARE @hr intDECLARE @StrEnd intDECLARE @Attachment varchar(255)DECLARE @return intDECLARE @Msg varchar(255)DECLARE @strBody varchar(8000)SET @strBody = '<!DOCTYPE HTML PUBLIC""-//IETF//DTD HTML//EN"">'SET @strBody = @strBody + '<HTML>'SET @strBody = @strBody + '<HEAD>'SET @strBody = @strBody + '<META HTTP-EQUIV=""Content-Type"" CONTENT=""text/html; CHARSET=iso-8859-1"">'SET @strBody = @strBody + '</HEAD>'SET @strBody = @strBody + '<BODY>'SET @strBody = @strBody + '<FONT FACE=""ARIAL"" COLOR=""#FF0000"">Test</FONT>'SET @strBody = @strBody + ' 'SET @strBody = @strBody + '</BODY>'SET @strBody = @strBody + '</HTML>'SET @strBody = REPLACE(@strBody, '""', '"')SELECT @From = ISNULL(@From, @@SERVERNAME)EXEC @hr = sp_OACreate 'CDONTS.NewMail', @object OUTIF @hr <> 0 GOTO ObjectErrorIF @strBody IS NOT NULLBEGINEXEC @hr = sp_OASetProperty @object, 'Body', @strBodyIF @hr <> 0 GOTO ObjectErrorEND IF @Cc IS NOT NULLBEGINEXEC @hr = sp_OASetProperty @object, 'Cc', @CcIF @hr <> 0 GOTO ObjectErrorEND IF @Bcc IS NOT NULLBEGINEXEC @hr = sp_OASetProperty @object, 'Bcc', @BccIF @hr <> 0 GOTO ObjectErrorENDIF @MailFormat <> 0BEGINEXEC @hr = sp_OASetProperty @object, 'MailFormat', @MailFormatIF @hr <> 0 GOTO ObjectErrorENDIF @BodyFormat <> 0BEGINEXEC @hr = sp_OASetProperty @object, 'BodyFormat', @BodyFormatIF @hr <> 0 GOTO ObjectErrorENDCREATE TABLE #FileExists (FileExists int, FileIsDir int, ParentDirExists int)WHILE ISNULL(LEN(@Attachments),0) > 0BEGINSELECT @StrEnd = CASE CHARINDEX(';', @Attachments)WHEN 0 THEN LEN(@Attachments)ELSE CHARINDEX(';', @Attachments) - 1END SELECT @Attachment = SUBSTRING(@Attachments, 1, @StrEnd)SELECT @Attachments = SUBSTRING(@Attachments, @StrEnd+2, LEN(@Attachments))DELETE #FileExistsINSERT #FileExistsEXEC master..xp_fileexist @AttachmentIF NOT EXISTS (SELECT * FROM #FileExists WHERE FileExists = 1)BEGINRAISERROR ('File %s does not exist. Message not sent.', 16, 1, @Attachment)RETURN 1END EXEC @hr = sp_OAMethod @object, 'AttachFile', NULL, @AttachmentIF @hr <> 0 GOTO ObjectErrorSELECT @Msg = 'File ' + @Attachment + ' attached.'PRINT @MsgENDEXEC @hr = sp_OAMethod @object, 'Send', NULL, @From, @To, @Subject, @Importance = @ImportanceIF @hr <> 0 GOTO ObjectErrorEXEC @hr = sp_OADestroy @objectIF @hr <> 0 GOTO ObjectErrorPRINT 'Message Sent Successfully'RETURN 0ObjectError:BEGINEXEC PrDisplayOAErrorInfo @object, @hrRETURN 1ENDGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOTha |
|