导航

SSIS 发送email.

Posted on 2008-10-21 15:39  Niko  阅读(456)  评论(0)    收藏  举报

今天学习ssis中的 send email task 遇到了一些问题。 mail服务器必须是 esmtp服务器, 由于公司没有esmtp服务器。所以就用了sql2005 中的 database mail 功能。

 

step1: 首先要启动database mail服务,当然默认的是enable.

 

setup2:

创建database mail account , 以下用的都是sp, 具体用法请参考http://msdn.microsoft.com/en-us/library/ms187646.aspx。 当然你也可以用gui操作, 在SQL Server Management Studio 下有一个Mangement->database mail. 双击就可以看到操作界面。

EXECUTE msdb.dbo.sysmail_add_account_sp

     @account_name = 'MyMailAccount',

     @description = 'Mail account for Database Mail',

     @email_address = 'makclaire@optonline.net',

     @display_name = 'MyAccount', 

     @username='makclaire@optonline.net', 

     @password='abc123', 

     @mailserver_name = 'smtp.optonline.net'  

 

setup3. 创建mail profile

EXECUTE msdb.dbo.sysmail_add_profile_sp

     @profile_name = 'MyMailProfile',

     @description = 'Profile used for database mail'

  

setup4.将创建的帐号添加到创建的mail profile

 EXECUTE msdb.dbo.sysmail_add_profileaccount_sp

     @profile_name = 'MyMailProfile',

     @account_name = 'MyMailAccount',

     @sequence_number = 1

 

setup5.

Use the sysmail_add_principalprofile procedure to grant the Database Mail profile access to the msdb public database role and to make the profile the default Database Mail profile.

EXECUTE msdb.dbo.sysmail_add_principalprofile_sp

@profile_name = 'MyMailProfile',

@principal_name = 'public',

@is_default = 1 ;

 

setup 6 : send mail

declare @body1 varchar(100)

set @body1 = 'Server :'+@@servername+ ' My First Database Email '

EXEC msdb.dbo.sp_send_dbmail @recipients='mak_999@yahoo.com',

@subject = 'My Mail Test',

@body = @body1,

@body_format = 'HTML' ;

 

DataBase mail 功能ok了

现在你在ssis中建立一 个 Execute SQL Task 将setup6的sql 拷贝到task的SQLStatement中,运行你的ssis就可以了。