[英]Save a single email attachment to BLOB using Chilkat and SQL Server
Chilkat.IMAP 9.5.0.75-ActiveX和SQL Server
在SQL Server中,我將從郵箱中提取所有新電子郵件到SQL表中,並保存UID,主題,正文和日期。 效果很好。
如果電子郵件中包含附件,則嘗試將它們另存為BLOB放在單獨的表中。 我成功使用SaveAllAttachments到文件系統,然后從文件系統導入它們。 但是,郵箱中的某些附件具有相同的文件名,我寧願使用內存中的文件,而不是先將它們保存到文件系統中。
我嘗試將Chilkat.Email GetAttachmentData直接使用到varbinary(max)變量中。 它大於4000字節,因此我嘗試使用表變量。 還嘗試以相同的方式使用Chilkat.IMAP FetchAttachmentBytes獲得相同的結果:
直接到varbinary(max)變量:
declare @bin_file varbinary(max) EXEC sp_OAMethod @email, 'GetAttachmentData', @bin_file out, @j
結果:
ChilkatLog: GetAttachmentData: DllDate: Aug 25 2018 ChilkatVersion: 9.5.0.75 UnlockPrefix: *************** Architecture: Little Endian; 64-bit Language: ActiveX / x64 VerboseLogging: 0 index: 0 numBytes: 426484 Success. --GetAttachmentData --ChilkatLog
@bin_file為空,即使它表示成功。 我認為這是由於4000字節的限制,但我不確定。
對於具有varbinary(max)列的表變量:
DECLARE @attach TABLE (attachment varbinary(max)) INSERT INTO @attach EXEC sp_OAMethod @email, 'GetAttachmentData', @j
結果:
ChilkatLog: GetHeaderField: ChilkatVersion: 9.5.0.75 --GetHeaderField --ChilkatLog
由於某種原因,日志顯示的是前一個命令而不是GetAttachmentData,就像SQL跳過了該語句一樣。 我正在使用相同的表變量方法來成功獲取電子郵件正文屬性。 無論我使用Chilkat.Email.GetAttachmentData還是Chilkat.IMAP.FetchAttachmentBytes,結果都是相同的。
這是整個腳本:
CREATE TABLE #email_uid (uid VARCHAR(1024))
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @imap int
EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Imap', @imap OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Anything unlocks the component and begins a fully-functional 30-day trial.
DECLARE @success int
EXEC sp_OAMethod @imap, 'UnlockComponent', @success OUT, 'hidden'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
-- Connect to an IMAP server.
-- Use TLS
EXEC sp_OASetProperty @imap, 'Ssl', 1
EXEC sp_OASetProperty @imap, 'Port', 993
EXEC sp_OAMethod @imap, 'Connect', @success OUT, 'hidden'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
-- Login
EXEC sp_OAMethod @imap, 'Login', @success OUT, 'hidden account name', 'hidden password'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
-- Select an IMAP mailbox
EXEC sp_OAMethod @imap, 'SelectMailbox', @success OUT, 'Inbox'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
DECLARE @messageSet int
-- fetch UIDs not sequence numbers.
DECLARE @fetchUids int
SELECT @fetchUids = 1
-- Return all messages.
DECLARE @allMsgs nvarchar(4000)
SELECT @allMsgs = 'ALL'
EXEC sp_OAMethod @imap, 'Search', @messageSet OUT, @allMsgs, @fetchUids
IF @messageSet Is NULL
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
-- Fetch the email headers into a bundle object:
DECLARE @bundle int
EXEC sp_OAMethod @imap, 'FetchHeaders', @bundle OUT, @messageSet
IF @bundle Is NULL
BEGIN
EXEC @hr = sp_OADestroy @messageSet
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
end
-- get all UID's from the inbox; loop through them and download the if we haven't downloaded that UID before
DECLARE @i int
EXEC sp_OAGetProperty @bundle, 'MessageCount', @iTmp0 OUT
SELECT @i = 0
WHILE @i <= @iTmp0 - 1
BEGIN
DECLARE @email int
EXEC sp_OAMethod @bundle, 'GetEmail', @email OUT, @i
EXEC sp_OAMethod @email, 'GetHeaderField', @sTmp0 OUT, 'ckx-imap-uid'
insert into #email_uid
select @sTmp0
EXEC @hr = sp_OADestroy @email
SELECT @i = @i + 1
end
-- delete UIDs we have already downloaded
delete a
from #email_uid a
inner join email b on b.uid = a.uid
declare @bUid int
select @bUid = 1
declare @s varchar(1024)
select @s = min(uid) from #email_uid
while exists (select 1 from #email_uid)
begin
-- Download the email by UID number.
EXEC sp_OAMethod @imap, 'FetchSingle', @email OUT, @s, @bUid
-- get number of attachments in the email
DECLARE @numAttach int
EXEC sp_OAMethod @imap, 'GetMailNumAttach', @numAttach OUT, @email
declare @subject varchar(1024)
, @date varchar(1024)
, @from varchar(1024)
, @body varchar(max)
-- Fetch a longer property string into a temp table:
DECLARE @tmp TABLE (emailBody ntext)
INSERT INTO @tmp EXEC sp_OAGetProperty @email, 'Body'
select @body = emailBody from @tmp
EXEC sp_OAGetProperty @email, 'Subject', @subject out
EXEC sp_OAGetProperty @email, 'From', @from out
EXEC sp_OAMethod @email, 'GetHeaderField', @date OUT, 'Date'
insert email ([UID],[Subject],[Date],[from],[Body])
select @s, @subject, @date, @from, @body
set @subject = null
set @from = null
set @date = null
set @body = null
DECLARE @j int
-- Loop through the attachments and insert them as BLOBS into attachment table
SELECT @j = 0
WHILE @j <= @numAttach - 1
BEGIN
DECLARE @filename nvarchar(4000)
EXEC sp_OAMethod @imap, 'GetMailAttachFilename', @filename OUT, @email, @j
PRINT @filename
DECLARE @attach TABLE (attachment varbinary(max))
INSERT INTO @attach EXEC sp_OAMethod @email, 'GetAttachmentData', @j
IF not exists (select 1 from @attach)
BEGIN
EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @imap
RETURN
end
--declare @bin_file varbinary(max)
--EXEC sp_OAMethod @email, 'GetAttachmentData', @bin_file out, @j
--if @bin_file is null
--BEGIN
--EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
--PRINT @sTmp0
--EXEC @hr = sp_OADestroy @email
--EXEC @hr = sp_OADestroy @imap
--RETURN
--END
insert into [dbo].[email_attachment] (UID,Filename,Attachment)
select @s, @filename, attachment
from @attach
delete @attach
select @j = @j + 1
END
EXEC @hr = sp_OADestroy @email
delete from #email_uid where uid = @s
select @s = min(uid) from #email_uid
end
-- Disconnect from the IMAP server.
EXEC sp_OAMethod @imap, 'Disconnect', @success OUT
EXEC @hr = sp_OADestroy @messageSet
EXEC @hr = sp_OADestroy @bundle
EXEC @hr = sp_OADestroy @imap
謝謝,達林
方法的名稱應為GetAttachmentData(而不是GetAttachmentBytes)。 參見https://www.chilkatsoft.com/refdoc/xChilkatEmailRef.html#method72
我不確定為什么使用名稱“ GetAttachmentBytes”時SQL不會拋出/返回錯誤。 似乎只是跳過了該聲明。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.