|
| 1 | +--- |
| 2 | +# required metadata |
| 3 | + |
| 4 | +title: DB Mail and Email Alerts with SQL Agent on Linux | Microsoft Docs |
| 5 | +description: This topic describes how to use DB Mail and Email Alerts with SQL Server on Linux |
| 6 | +author: meet-bhagdev |
| 7 | +ms.author: meetb |
| 8 | +manager: jhubbard |
| 9 | +ms.date: 07/17/2017 |
| 10 | +ms.topic: article |
| 11 | +ms.prod: sql-linux |
| 12 | +ms.technology: database-engine |
| 13 | +ms.assetid: tbd |
| 14 | + |
| 15 | +# optional metadata |
| 16 | + |
| 17 | +# keywords: "DB Main, SQL Agent, SQL Agent DB Mail, DB Mail with SQL Server on Linux, SQL Agent alerts on Linux" |
| 18 | +# ROBOTS: "" |
| 19 | +# audience: "" |
| 20 | +# ms.devlang: "" |
| 21 | +# ms.reviewer: "" |
| 22 | +# ms.suite: "" |
| 23 | +# ms.tgt_pltfrm: "" |
| 24 | +# ms.custom: "" |
| 25 | + |
| 26 | +--- |
| 27 | +# DB Mail and Email Alerts with SQL Agent on Linux |
| 28 | +[!INCLUDE[tsql-appliesto-sslinx-only_md](../../docs/includes/tsql-appliesto-sslinx-only_md.md)] |
| 29 | + |
| 30 | +The following steps show you how to set up DB Mail and use it with SQL Server Agent (**mssql-server-agent**) on Linux. |
| 31 | + |
| 32 | +> [!NOTE] |
| 33 | +> To use DB Mail with SQL Server on Linux, you need to use SQL Server 2017 RC1 or later. |
| 34 | +
|
| 35 | +## Prerequisites |
| 36 | +- SQL Server 2017 RC1 and above |
| 37 | +- SQL Server Agent v14.0.800.90-2 and above (if you plan to use email for alerts) |
| 38 | + |
| 39 | +## 1. Enable DB Mail |
| 40 | + |
| 41 | +```sql |
| 42 | +USE master |
| 43 | +GO |
| 44 | +sp_configure 'show advanced options',1 |
| 45 | +GO |
| 46 | +RECONFIGURE WITH OVERRIDE |
| 47 | +GO |
| 48 | +sp_configure 'Database Mail XPs', 1 |
| 49 | +GO |
| 50 | +RECONFIGURE |
| 51 | +GO |
| 52 | +``` |
| 53 | + |
| 54 | +## 2. Create a new account |
| 55 | +```sql |
| 56 | +EXECUTE msdb.dbo.sysmail_add_account_sp |
| 57 | +@account_name = 'SQLAlerts', |
| 58 | +@description = 'Account for Automated DBA Notifications', |
| 59 | +@email_address = 'sqlagenttest@gmail.com', |
| 60 | +@replyto_address = 'sqlagenttest@gmail.com', |
| 61 | +@display_name = 'SQL Agent', |
| 62 | +@mailserver_name = 'smtp.gmail.com', |
| 63 | +@port = 587, |
| 64 | +@enable_ssl = 1, |
| 65 | +@username = 'sqlagenttest@gmail.com', |
| 66 | +@password = '<password>' |
| 67 | +GO |
| 68 | +``` |
| 69 | + |
| 70 | +## 3. Create a default profile |
| 71 | + |
| 72 | +```sql |
| 73 | +EXECUTE msdb.dbo.sysmail_add_profile_sp |
| 74 | +@profile_name = 'default', |
| 75 | +@description = 'Profile for sending Automated DBA Notifications' |
| 76 | +GO |
| 77 | +``` |
| 78 | + |
| 79 | +## 4. Add the Database Mail account to a Database Mail profile |
| 80 | +```sql |
| 81 | +EXECUTE msdb.dbo.sysmail_add_principalprofile_sp |
| 82 | +@profile_name = 'default', |
| 83 | +@principal_name = 'public', |
| 84 | +@is_default = 1 ; |
| 85 | + ``` |
| 86 | + |
| 87 | +## 5. Add account to profile |
| 88 | +```sql |
| 89 | +EXECUTE msdb.dbo.sysmail_add_profileaccount_sp |
| 90 | +@profile_name = 'default', |
| 91 | +@account_name = 'SQLAlerts', |
| 92 | +@sequence_number = 1; |
| 93 | + ``` |
| 94 | + |
| 95 | +## 6. Send test email |
| 96 | +> [!NOTE] |
| 97 | +> You might have to go to your email client and enable the "allow less secure clients to send mail." Not all clients recognize DB Mail as an email daemon. |
| 98 | +
|
| 99 | +``` |
| 100 | +EXECUTE msdb.dbo.sp_send_dbmail |
| 101 | +@profile_name = 'default', |
| 102 | +@recipients = 'recipient-email@gmail.com', |
| 103 | +@Subject = 'Testing DBMail', |
| 104 | +@Body = 'This message is a test for DBMail' |
| 105 | +GO |
| 106 | +``` |
| 107 | + |
| 108 | +## 7. Set DB Mail Profile using mssql-conf or environment variable |
| 109 | +You can use the mssql-conf utility or environment variables to register your DB Mail profile. In this case, let's call our profile default. |
| 110 | + |
| 111 | +```bash |
| 112 | +# via mssql-conf |
| 113 | +sudo /opt/mssq/bin/mssql-conf set sqlagent.databasemailprofile default |
| 114 | +# via environment variable |
| 115 | +MSSQL_AGENT_EMAIL_PROFILE=default |
| 116 | +``` |
| 117 | + |
| 118 | +## 8. Set up an operator for SQLAgent job notifications |
| 119 | + |
| 120 | +```sql |
| 121 | +EXEC msdb.dbo.sp_add_operator |
| 122 | +@name=N'JobAdmins', |
| 123 | +@enabled=1, |
| 124 | +@email_address=N'recipient-email@gmail.com', |
| 125 | +@category_name=N'[Uncategorized]' |
| 126 | +GO |
| 127 | +``` |
| 128 | + |
| 129 | +## 9. Send email when 'Agent Test Job’ succeeds |
| 130 | + |
| 131 | +``` |
| 132 | +EXEC msdb.dbo.sp_update_job |
| 133 | +@job_name='Agent Test Job', |
| 134 | +@notify_level_email=1, |
| 135 | +@notify_email_operator_name=N'JobAdmins' |
| 136 | +GO |
| 137 | +``` |
| 138 | + |
| 139 | +## Next steps |
| 140 | +For more information on how to use SQL Server Agent to create, schedule, and run jobs, see [Run a SQL Server Agent job on Linux](sql-server-linux-run-sql-server-agent-job.md). |
0 commit comments