Advertisement
Guest User

Untitled

a guest
Jan 26th, 2017
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. -- Enable service broker in the MSDB database.
  2. USE [master]
  3. GO
  4. ALTER DATABASE [MSDB] SET ENABLE_BROKER WITH NO_WAIT
  5. GO
  6.  
  7. --Enabling Database Mail
  8. sp_configure 'show advanced options',1
  9. reconfigure;
  10. GO
  11.  
  12. -- Enable the db mail feature at server level
  13. sp_configure 'Database Mail XPs',1
  14. reconfigure;
  15.  
  16. --Creating a Profile
  17. EXECUTE msdb.dbo.sysmail_add_profile_sp
  18. @profile_name = 'SQLProfile',
  19. @description = 'Mail Service for SQL Server' ;
  20.  
  21. -- Create a Mail account
  22. EXECUTE msdb.dbo.sysmail_add_account_sp
  23. @account_name = 'SQL_Email_Account',
  24. @email_address = 'somebody@gmail.com',
  25. @mailserver_name = 'smtp.googlemail.com',
  26. @port=465,
  27. @enable_ssl=1,
  28. @username='somebody@gmail.com',
  29. @password='Emailid password'
  30. -- TODO ENSURE VALID PASSWORD FOR THE ACCOUNT IS ENTERED ABOVE
  31.  
  32. -- Adding the account to the profile
  33. EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
  34. @profile_name = 'SQLProfile',
  35. @account_name = 'SQL_Email_Account',
  36. @sequence_number =1 ;
  37.  
  38. -- Granting access to the profile to the DatabaseMailUserRole of MSDB
  39. EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
  40. @profile_name = 'SQLProfile',
  41. @principal_id = 0,
  42. @is_default = 1;
  43.  
  44. --Sending Test Mail
  45. -- See https://msdn.microsoft.com/en-us/library/ms190307.aspx for all arguments
  46. EXEC msdb.dbo.sp_send_dbmail
  47. @profile_name = 'SQLProfile',
  48. @recipients = 'somebodyelse@yahoo.co.uk',
  49. @body = 'Database Mail Testing',
  50. @subject = 'Database Mail from SQL Server';
  51.  
  52. select * from msdb.dbo.sysmail_event_log
  53.  
  54. USE [master]
  55. GO
  56.  
  57. -- Create a Database Mail account 2
  58. EXEC msdb.dbo.sysmail_add_account_sp
  59. @account_name = 'SQL_Email_Account 2',
  60. @description = 'Alternative Mail account',
  61. @email_address = 'somebody3@gmx.co.uk',
  62. @display_name = 'Alternate GMX',
  63. @port=465,
  64. @enable_ssl=1,
  65. @username='somebody3@gmx.co.uk',
  66. @password='ValidPassword',
  67. @mailserver_name = 'mail.gmx.com';
  68. GO
  69.  
  70. -- Add the account 2 to the profile
  71. EXEC msdb.dbo.sysmail_add_profileaccount_sp
  72. @profile_name = 'SQLProfile',
  73. @account_name = 'SQL_Email_Account 2',
  74. @sequence_number = 2 ;
  75.  
  76. -- Make one not the default
  77. EXECUTE msdb.dbo.sysmail_update_principalprofile_sp
  78. @profile_name = 'SQLProfile',
  79. @principal_name = 'public',
  80. @is_default = 0 ;
  81.  
  82. -- Show the new default profile
  83. EXEC msdb.dbo.sysmail_help_principalprofile_sp
  84.  
  85. Use msdb
  86. Go
  87.  
  88. select * from sysmail_profile
  89. select * from sysmail_account
  90. select * from sysmail_profileaccount where profile_id=1
  91. select * from sysmail_server
  92. EXEC msdb.dbo.sysmail_help_account_sp;
  93. EXEC msdb.dbo.sysmail_help_profileaccount_sp @profile_name = 'SQLProfile';
  94. EXEC msdb.sys.sp_helprolemember 'DatabaseMailUserRole';
  95. EXEC msdb.dbo.sysmail_help_principalprofile_sp;
  96. EXEC msdb.dbo.sysmail_help_status_sp;
  97. exec [dbo].[sysmail_configure_sp] 'LoggingLevel', 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement