SQL SERVER Mail

来源:互联网 发布:java项目中碰到的难点 编辑:程序博客网 时间:2024/05/16 01:10

-- =================================================

-- 启用数据库邮件功能

-- =================================================

EXEC sys.sp_configure N'show advanced options', 1

RECONFIGURE;

 

EXEC sys.sp_configure 'Database Mail XPs', 1;

RECONFIGURE;

 

 

-- =================================================

-- 配置

-- =================================================

DECLARE

    @account_name sysname,

    @profile_name sysname;

SELECT

    @account_name = N'GmailTest@gmail.com',  -- 数据库邮件帐号

    @profile_name = N'gmail';                -- 数据库邮件配置文件名

 

-- 数据库邮件帐号

EXEC msdb.dbo.sysmail_add_account_sp

    @account_name = @account_name,

    @email_address = N'GmailTest@gmail.com',  -- Email 地址

    @display_name = N'gmail test',            -- 回复地址

    @mailserver_name = N'smtp.gmail.com',     -- Gmail smtp 服务器地址

    @port = 25,                               -- 端口号(对于Gmail, 如果 25 不通则改用 456)

    @username = N'GmailTest@gmail.com',       -- Gmail 邮件地址

    @password = N'abc.123',                   -- Gmail 邮件帐号密码

    @use_default_credentials = 0,

    @enable_ssl = 1;                          -- 启用SSL 通讯加密

 

-- 数据库邮件配置文件

EXEC msdb.dbo.sysmail_add_profile_sp

    @profile_name = @profile_name;

   

-- 把数据库邮件帐号添加到邮件配置文件中

EXEC msdb.dbo.sysmail_add_profileaccount_sp

    @profile_name = @profile_name,

    @account_name = @account_name,

    @sequence_number = 1;

 

-- 授予任意用户使用数据库邮件配置文件的权限

EXEC msdb.dbo.sysmail_add_principalprofile_sp

    @principal_name = N'guest',

    @profile_name = @profile_name,

    @is_default = 0;

 

 

-- =================================================

-- 发送邮件

-- =================================================

EXEC msdb.dbo.sp_send_dbmail

    @profile_name = @profile_name,

    @recipients = N'GmailTest@gmail.com',      -- 收件人地址

    @subject = N'test mail from database',     -- 邮件主题

    @body = N'this is a test mail';            -- 邮件内容

 

 

-- =================================================

-- 删除配置

-- =================================================

/* --

DECLARE

    @account_name sysname,

    @profile_name sysname;

SELECT

    @account_name = N'GmailTest@gmail.com',

    @profile_name = N'gmail';

 

-- 从邮件配置文件中删除数据库邮件帐号

EXEC msdb.dbo.sysmail_delete_profileaccount_sp

    @profile_name = @profile_name,

    @account_name = @account_name;

   

-- 删除数据库邮件帐号

EXEC msdb.dbo.sysmail_delete_account_sp

    @account_name = @account_name;

 

-- 删除数据库邮件配置文件

EXEC msdb.dbo.sysmail_delete_profile_sp

    @profile_name = @profile_name;

 

 

-- 禁用数据库邮件功能

EXEC sys.sp_configure 'Database Mail XPs', 0;

RECONFIGURE;

 

EXEC sys.sp_configure N'show advanced options', 0

RECONFIGURE;

-- */

原创粉丝点击