2010-06-22 4 views
1

BLOB로 저장된 파일을 이메일의 일부로 첨부하는 방법에 대한 많은 정보를 찾을 수 없습니다.SQL 저장 프로 시저를 사용하여 전자 메일에 파일을 첨부하는 방법은 무엇입니까?

파일 시스템 (C : \ temp ...)의 파일을 DBMAIL 또는 사용자 지정 저장 프로 시저에서 설정중인 전자 메일에 첨부 할 수 있다는 것을 알고 있습니다. 그러나 테이블에 이진 객체로 저장된 PDF와 같은 것을 첨부하는 것에 대해서는 언급하지 않았습니다.

내가 파일로 쿼리를 첨부 할 수있는 곳을 알지만, 내가 찾고있는 곳이라고 생각하지 않습니다.

우리는 프로그램을 통해 프로그램을 통해이 작업을 수행하지만 트리거 또는 응용 프로그램의 서버 측 코드를 통해이 SP를 실행할 수 있어야합니다.

대개 이진 객체가 처리 방법을 알기 위해 브라우저 나 메일 객체에 대해 콘텐츠 유형을 필요로하므로 다른 질문을해야합니까?

답변

1

솔루션은 db 필드에 저장된 이진 객체를 첨부 할 수 없으므로 스키마를 약간 변경하여 이진 파일의 경로를 저장하십시오. 데이터베이스 서버에서 .net clr을 활성화 할 수 있다면 더 많은 옵션이 제공됩니다.

use master 
go 
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_send_cdosysmail]') 
and objectproperty(id, N'isprocedure') = 1) 
drop procedure [dbo].[usp_send_cdosysmail] 
go 

create procedure usp_send_cdosysmail 
@from varchar(500) , 
@to varchar(500) , 
@subject varchar(500), 
@body nvarchar(max) , 
@smtpserver varchar(25), 
@bodytype varchar(10) , 
@attachment varchar(100)= ' ' 
as 
declare @imsg int 
declare @hr int 
declare @source varchar(255) 
declare @description varchar(500) 
declare @output varchar(1000) 
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', @smtpserver 

exec @hr = sp_oamethod @imsg, 'configuration.fields.update', null 
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, @bodytype, @body 

-- Attachments... 

IF @attachment IS NOT NULL AND LEN(@attachment) > 0 BEGIN 
    Declare @files table(fileid int identity(1,1),[file] varchar(255)) 
    Declare @file varchar(255) 
    Declare @filecount int ; set @filecount=0 
    Declare @counter int ; set @counter = 1 
    DECLARE @outVar INT 
    SET @outVar = NULL 

     INSERT @files SELECT cValue FROM master..fn_split(@attachment,',') 
     SELECT @[email protected]@ROWCOUNT 

     WHILE @counter<(@filecount+1) 
     BEGIN 
       SELECT @file = [file] 
       FROM @files 
       WHERE [email protected] 

       EXEC @hr = sp_OAMethod @imsg, 'AddAttachment',@outVar OUT, @file 

       SET @[email protected]+1 
     END 
END 

exec @hr = sp_oamethod @imsg, 'send', null 

-- sample error handling. 
if @hr <>0 
select @hr 
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 
exec @hr = sp_oadestroy @imsg 
go 
set quoted_identifier off 
go 
set ansi_nulls on 
go 

sp_configure 'Ole Automation Procedures', 1 
RECONFIGURE 
GO