forked from bulentgucuk/DBA-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCreate Database Mail Account And Profile.sql
106 lines (85 loc) · 4.15 KB
/
Create Database Mail Account And Profile.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
--================================================================
-- DATABASE MAIL CONFIGURATION
--================================================================
/****
The script is broken down into the following sections:
Creation of Database Mail Account
Creation of Database Mail Profile
Enable Database Mail for the SQL Server instance
Returning list of accounts and profiles for the SQL Server instance
Send test Database Mail
Assignment of a Database Mail profile to the SQL Server Agent
****/
--==========================================================
-- Create a Database Mail account
--==========================================================
EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = '<account_name, DBM account name, Database Mail Primary Account>',
@description = '<description, , SQL Server Notification Service>',
@email_address = '<email_address, email address for DBM. Does not need a valid mail account ,>',
@replyto_address = '<replyto_address, reply email address for DBM. Does not need a valid mail account ,>',
@display_name = '<display_name, friendly name for emails sent via DBM, Database Mail Account>',
@mailserver_name = '<mailserver_name, smtp mail server name,>',
@port = '<port_number, port number of the mailserver, 25>';
--==========================================================
-- Create a Database Mail Profile
--==========================================================
DECLARE @profile_id INT, @profile_description sysname;
SELECT @profile_id = COALESCE(MAX(profile_id),1) FROM msdb.dbo.sysmail_profile
SELECT @profile_description = 'Database Mail Profile for ' + @@servername
EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = '<profile_name, DBM profile name, Database Mail Primary Profile>',
@description = @profile_description;
-- Add the account to the profile
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = '<profile_name, DBM profile name, Database Mail Primary Profile>',
@account_name = '<account_name, DBM account name, Database Mail Primary Account>',
@sequence_number = @profile_id;
-- Grant access to the profile to the DBMailUsers role
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
@profile_name = '<profile_name, DBM profile name, Database Mail Primary Profile>',
@principal_id = 0,
@is_default = 1 ;
--==========================================================
-- Enable Database Mail
--==========================================================
USE master;
GO
sp_CONFIGURE 'show advanced', 1
GO
RECONFIGURE
GO
sp_CONFIGURE 'Database Mail XPs', 1
GO
RECONFIGURE
GO
--EXEC master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'DatabaseMailProfile', N'REG_SZ', N''
--EXEC master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'UseDatabaseMail', N'REG_DWORD', 1
--GO
EXEC msdb.dbo.sp_set_sqlagent_properties @email_save_in_sent_folder = 0
GO
--==========================================================
-- Review Outcomes
--==========================================================
SELECT * FROM msdb.dbo.sysmail_profile;
SELECT * FROM msdb.dbo.sysmail_account;
GO
--==========================================================
-- Test Database Mail
--==========================================================
DECLARE @sub VARCHAR(100)
DECLARE @body_text NVARCHAR(MAX)
SELECT @sub = 'Test from New SQL install on ' + @@servername
SELECT @body_text = N'This is a test of Database Mail.' + CHAR(13) + CHAR(13) + 'SQL Server Version Info: ' + CAST(@@version AS VARCHAR(500))
EXEC msdb.dbo.[sp_send_dbmail]
@profile_name = '<profile_name, DBM profile name, Database Mail Primary Profile>'
, @recipients = '<test_email_address, email address to send test email,>'
, @subject = @sub
, @body = @body_text
--================================================================
-- SQL Agent Properties Configuration
--================================================================
EXEC msdb.dbo.sp_set_sqlagent_properties
@databasemail_profile = '<profile_name, DBM profile name, Database Mail Primary Profile>'
, @use_databasemail=1
GO