1_DatabaseMail_Config.sql 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. sp_configure 'show advanced options', 1
  2. RECONFIGURE WITH OVERRIDE
  3. GO
  4. sp_configure 'Database Mail XPs', 1
  5. RECONFIGURE WITH OVERRIDE
  6. GO
  7. USE [msdb]
  8. GO
  9. -------------------------------------------------------------
  10. -- Database Mail Simple Configuration Template.
  11. --
  12. -- This template creates a Database Mail profile, an SMTP account and
  13. -- associates the account to the profile.
  14. -- The template does not grant access to the new profile for
  15. -- any database principals. Use msdb.dbo.sysmail_add_principalprofile
  16. -- to grant access to the new profile for users who are not
  17. -- members of sysadmin.
  18. -------------------------------------------------------------
  19. DECLARE @profile_name sysname,
  20. @account_name sysname,
  21. @SMTP_servername sysname,
  22. @email_address NVARCHAR(128),
  23. @display_name NVARCHAR(128),
  24. @port_number int,
  25. @desc_p NVARCHAR(128),
  26. @desc_a NVARCHAR(128),
  27. @customoper sysname;
  28. -- Profile name. Replace with the name for your profile
  29. SET @profile_name = 'Database Administration Profile';
  30. -- Account and SQL Operator information. Replace with the information for your account.
  31. SET @account_name = 'Database Administration Profile';
  32. SET @SMTP_servername = 'SERVER_FQDN';
  33. SET @email_address = 'user@domain';
  34. SET @display_name = 'user';
  35. SET @port_number = 25;
  36. SET @desc_p = 'Mail account used by DBA staff';
  37. SET @desc_a = 'Mail account used by DBA staff';
  38. SET @customoper = 'SQLAdmins'
  39. -- Verify the specified account and profile do not already exist.
  40. IF EXISTS (SELECT * FROM msdb.dbo.sysmail_profile WHERE name = @profile_name)
  41. BEGIN
  42. RAISERROR('The specified Database Mail profile (Database Administration Profile) already exists.', 16, 1);
  43. GOTO done;
  44. END;
  45. IF EXISTS (SELECT * FROM msdb.dbo.sysmail_account WHERE name = @account_name )
  46. BEGIN
  47. RAISERROR('The specified Database Mail account (Database Administration Profile) already exists.', 16, 1) ;
  48. GOTO done;
  49. END;
  50. -- Start a transaction before adding the account and the profile
  51. BEGIN TRANSACTION ;
  52. DECLARE @rv int;
  53. -- Add the account
  54. EXECUTE @rv=msdb.dbo.sysmail_add_account_sp
  55. @account_name = @account_name,
  56. @email_address = @email_address,
  57. @replyto_address = NULL,
  58. @display_name = @display_name,
  59. @mailserver_name = @SMTP_servername,
  60. @mailserver_type = 'SMTP',
  61. @port = @port_number,
  62. @description = @desc_a,
  63. @username = NULL,
  64. @password = NULL,
  65. @use_default_credentials = 0,
  66. @enable_ssl = 0;
  67. IF @rv<>0
  68. BEGIN
  69. RAISERROR('Failed to create the specified Database Mail account (Database Administration Profile).', 16, 1) ;
  70. GOTO done;
  71. END
  72. -- Add the profile
  73. EXECUTE @rv=msdb.dbo.sysmail_add_profile_sp
  74. @profile_name = @profile_name,
  75. @description = @desc_p;
  76. IF @rv<>0
  77. BEGIN
  78. RAISERROR('Failed to create the specified Database Mail profile (Database Administration Profile).', 16, 1);
  79. ROLLBACK TRANSACTION;
  80. GOTO done;
  81. END;
  82. -- Associate the account with the profile.
  83. EXECUTE @rv=msdb.dbo.sysmail_add_profileaccount_sp
  84. @profile_name = @profile_name,
  85. @account_name = @account_name,
  86. @sequence_number = 1 ;
  87. IF @rv<>0
  88. BEGIN
  89. RAISERROR('Failed to associate the speficied profile with the specified account (Database Administration Profile).', 16, 1) ;
  90. ROLLBACK TRANSACTION;
  91. GOTO done;
  92. END;
  93. -- Grant permission to public.
  94. EXECUTE @rv=msdb.dbo.sysmail_add_principalprofile_sp
  95. @principal_id = 0,
  96. @profile_name = @profile_name,
  97. @is_default = 1;
  98. IF @rv<>0
  99. BEGIN
  100. RAISERROR('Failed to grant permission for [public] role to use Database Mail profile (Database Administration Profile).', 16, 1) ;
  101. ROLLBACK TRANSACTION;
  102. GOTO done;
  103. END;
  104. EXECUTE @rv=master.dbo.sp_MSsetalertinfo @failsafeoperator=N'Administrator', @notificationmethod=1
  105. IF @rv<>0
  106. BEGIN
  107. RAISERROR('Failed to set default operator (Administrator).', 16, 1) ;
  108. ROLLBACK TRANSACTION;
  109. GOTO done;
  110. END;
  111. EXECUTE @rv=msdb.dbo.sp_set_sqlagent_properties @email_save_in_sent_folder=1
  112. IF @rv<>0
  113. BEGIN
  114. RAISERROR('Failed to set SQL Agent property (email_save_in_sent_folder).', 16, 1) ;
  115. ROLLBACK TRANSACTION;
  116. GOTO done;
  117. END;
  118. EXECUTE @rv=master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'UseDatabaseMail', N'REG_DWORD', 1
  119. IF @rv<>0
  120. BEGIN
  121. RAISERROR('Failed to set SQL Agent property (UseDatabaseMail).', 16, 1) ;
  122. ROLLBACK TRANSACTION;
  123. GOTO done;
  124. END;
  125. EXECUTE @rv=master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'DatabaseMailProfile', N'REG_SZ', N'Database Administration Profile'
  126. IF @rv<>0
  127. BEGIN
  128. RAISERROR('Failed to set SQL Agent property (DatabaseMailProfile).', 16, 1) ;
  129. ROLLBACK TRANSACTION;
  130. GOTO done;
  131. END;
  132. --Create operator
  133. IF EXISTS (SELECT name FROM msdb.dbo.sysoperators WHERE name = @customoper)
  134. EXEC msdb.dbo.sp_delete_operator @name=@customoper
  135. EXEC msdb.dbo.sp_add_operator @name=@customoper,
  136. @enabled=1,
  137. @weekday_pager_start_time=90000,
  138. @weekday_pager_end_time=180000,
  139. @saturday_pager_start_time=90000,
  140. @saturday_pager_end_time=180000,
  141. @sunday_pager_start_time=90000,
  142. @sunday_pager_end_time=180000,
  143. @pager_days=0,
  144. @email_address=@email_address,
  145. @category_name=N'[Uncategorized]'
  146. COMMIT TRANSACTION;
  147. done:
  148. GO
  149. --To check service broker status for MSDB.
  150. DECLARE @BROSTAT int
  151. SELECT @BROSTAT = is_broker_enabled FROM sys.databases WHERE name='msdb'
  152. --If the above query returns 1, the service broker is enabled in MSDB. If not run the below query to enable service broker in MSDB, we need to enable this because database mail works with this.
  153. IF @BROSTAT <> 1
  154. BEGIN
  155. EXEC sp_executesql N'ALTER DATABASE msdb SET enable_broker'
  156. END
  157. --Once its done, run the below query in MSDB to enable the queue ExternalMailQueue
  158. EXEC msdb..sysmail_start_sp
  159. ALTER QUEUE ExternalMailQueue WITH status = on
  160. GO