SQLserver发送邮件

来源:互联网 发布:网络拓扑图用什么软件 编辑:程序博客网 时间:2024/04/28 08:37
 

--server 配置
sp_configure 'show advanced options', 1
go
RECONFIGURE
go
sp_configure 'SQL Mail XPs', 1
go
RECONFIGURE
go

sp_configure 'Database Mail XPs', 1
go
RECONFIGURE
go

--1 基本配置ファイルを作成
EXECUTE msdb.dbo.sysmail_help_profile_sp
--2 アカウントを作成
EXECUTE msdb.dbo.sysmail_help_account_sp ;
--3 基本配置ファイルとアカウント結合し順番を指定
EXECUTE msdb.dbo.sysmail_help_profileaccount_sp
--4 既定のアカウントを指定
EXECUTE msdb.dbo.sysmail_help_principalprofile_sp ;
--5 システムパラメータを指定
EXECUTE msdb.dbo.sysmail_help_configure_sp ;

 

exec dbo.sp_send_dbmail
@profile_name='myMail',
@recipients='XXX@163.com',
@subject='メールテスト',
@body='hello'

exec dbo.sp_send_dbmail
select * from sysmail_log
select * from sysmail_sentitems

 

use master
go
exec sp_configure 'show advanced options',1
go
reconfigure
go
exec sp_configure 'Database mail XPs',1
go
reconfigure
go

--1 基本配置ファイルを作成
EXECUTE msdb.dbo.sysmail_help_profile_sp '1','fan'
--2 アカウントを作成
EXECUTE msdb.dbo.sysmail_help_account_sp ;
--3 基本配置ファイルとアカウント結合し順番を指定
EXECUTE msdb.dbo.sysmail_help_profileaccount_sp
--4 既定のアカウントを指定
EXECUTE msdb.dbo.sysmail_help_principalprofile_sp ;
--5 システムパラメータを指定
EXECUTE msdb.dbo.sysmail_help_configure_sp

exec msdb.dbo.sysmail_add_profile_sp 'fan',''

EXECUTE msdb.dbo.sysmail_add_account_sp
    @account_name = 'fan_mail',
    @email_address = 'xxx@dl.cn',
    @display_name = 'fan',
    @mailserver_name = 'mail.dl.cn',
    @username= 'xxx@dl.cn',
    @password ='fh13084178517',
    @use_default_credentials ='0';

EXECUTE msdb.dbo.sysmail_add_profileaccount_sp 1,null,1,null,1

EXECUTE msdb.dbo.sysmail_add_principalprofile_sp 1,null,1,null,1

EXEC msdb.dbo.sp_send_dbmail
 @profile_name = 'fan',
@recipients = 'xxx@dl.cn',
@subject = 'myTest',
@query = 'select * from test.dbo.a',
@attach_query_result_as_file = 1,
@query_attachment_filename = 'a.txt',
@body='my love ,hhhhh'

exec msdb.dbo.sp_send_dbmail
select * from msdb.dbo.sysmail_log
select * from msdb.dbo.sysmail_sentitems

select * from sysmail_log
select * from sysmail_sentitems

EXEC msdb.dbo.sp_send_dbmail
 @profile_name = 'fan',
@copy_recipients='xxx@163.com',
@recipients = 'xxx@dl.cn',
@blind_copy_recipients ='xxx@vip.163.com',
@subject = 'myTest',
@file_attachments = 'c:\5.txt.txt;c:\a\5.txt.txt',
@body='my love ,hhhhh',
@importance = 'High'

 

原创粉丝点击