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
谢谢, 达林
答案 0 :(得分:0)
方法的名称应为GetAttachmentData(而不是GetAttachmentBytes)。参见https://www.chilkatsoft.com/refdoc/xChilkatEmailRef.html#method72
我不确定为什么使用名称“ GetAttachmentBytes”时SQL不会抛出/返回错误。似乎只是跳过了该声明。