使用SQL Agent中安排的SSIS包通过电子邮件发送SSRS报告

时间:2017-11-14 19:27:07

标签: sql-server email reporting-services ssis

我有一个SSIS包,可以在excel中导出SSRS报告(报告有参数)并将其存储在磁盘上。然后我可以给它发电子邮件。但是,我需要使用SSIS包发送SSRS报告,而不将其存储在光盘上。此外,电子邮件地址必须来自数据库表,数据库表将按表“地址类型”(主要地址,区域地址等)中的字段进行过滤,这将取决于报告中的选择参数。 我今天的目标是通过电子邮件将存储的文件发送到至少一个收件人。因此,这里是ScriptTask,我用它在SSRS中通过创建的SSIS包中的另一个脚本任务发送存储在光盘上的文件。脚本给出下一个错误“”,如果我取消注释字符串'objmes.AddAttachment =“e:\ report.xls”

,也会出现错误
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.ComponentModel
Imports System.Diagnostics
<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
    Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    Enum ScriptResults
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    End Enum


    Public Sub Main()
        Dim objmes
        objmes = CreateObject("CDO.Message")
        objmes.From = "e-mail1"
        objmes.To = "e-mail2"
        objmes.Subject = "my first E-mail"
        objmes.TextBody = "Hello world"
        'objmes.AddAttachment = "e:\report.xls"
        objmes.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.*****.com"
        objmes.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        objmes.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        objmes.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "email1"
        objmes.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "*******"
        objmes.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        objmes.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
        objmes.Configuration.Fields.Update()
        objmes.Send()

    End Sub

End Class

我将不胜感激任何帮助!

1 个答案:

答案 0 :(得分:0)

关于您想要根据地址类型

获取电子邮件的第二个问题

就像这样

CREATE PROCEDURE [dbo].[mail_generation]

-- the input parameters
@addresstypekey as uniqueidentifier

AS
BEGIN
declare @email nvarchar(120)

declare crs cursor for 
select c.email
from  contacts c inner join addresstype ad on c.addresstypekey = ad.addresstypekey
where ad.addresstypekey = @addresstypekey

open crs
fetch next from crs into @email

while @@FETCH_STATUS = 0
begin

   EXEC msdb.dbo.sp_send_dbmail @recipients = @email

   fetch next from crs into @email

end

close crs 
deallocate crs

END

所以你有一个输入参数,它是地址类型(主地址,区域地址等)

选择指定类型的电子邮件

然后发送邮件给每个收件人都有这种类型的地址。