DBAs - Do you use Database Mail (not SQL Mail)?

Updated by Brook Jeynes [SSW] 1 year ago. See history

123

SQL Server includes Database Mail (it was a new feature released back in 2005 as a replacement for SQL Mail). Database Mail is a great feature as it allows:

- HTML messages natively supported - so there's no need to use 3rd party dlls anymore
- Communication direct with SMTP server - There's no need for outlook or MAPI profiles on server
- Multiple profiles and accounts supported to specify multiple SMTP servers or different email infrastructure situations
- SQL Server queues messages even when the external mailing process fails
- High security - users and roles have to be granted permission to send mail
- Logging and auditing
- Attachment size regulations and file extension requirements
<imageEmbed
  alt="Image"
  size="large"
  showBorder={false}
  figureEmbed={{
    preset: "badExample",
    figure: 'Bad example - Using SQL Mail',
    shouldDisplay: true
  }}
  src="/uploads/rules/use-database-mail-not-sql-mail/SQLDatabases_SQLMail.png"
/>

```sql
EXEC master.dbo.xp_smtp_sendmail
@FROM = N'your@email.com',
@FROM_NAME = N'Sophie Belle',
@TO = 'recipient@email.com',
@subject = 'Vendor List',
@message = 'The list of vendors is attached.',
@type = N'text/html',
@server = N'mail.company.com.au'
```

<figureEmbed figureEmbed={{
  preset: "badExample",
  figure: 'Figure: Bad example - Avoid using SQL Mail - you need to have Outlook on the server and there is no built-in logging',
  shouldDisplay: true
} } />


<imageEmbed
  alt="Image"
  size="large"
  showBorder={false}
  figureEmbed={{
    preset: "goodExample",
    figure: 'Good example -  Use Database Mail',
    shouldDisplay: true
  }}
  src="/uploads/rules/use-database-mail-not-sql-mail/SqlDatabaseMail01.png"
/>

```sql
USE msdb
Execute dbo.sp_send_dbmail
@profile_name = 'UTS',
@recipients = 'your@email.com,
@body = 'The list of vendors is attached.',
@query = 'USE AdventureWorks; SELECT VendorID, Name FROM Purchasing.Vendor',
@subject = 'Vendor List',
@attach_query_result_as_file = 1
```

<figureEmbed figureEmbed={{
  preset: "goodExample",
  figure: 'Figure: Good example - Use database mail for scalability, built-in logging and HTML capability',
  shouldDisplay: true
} } />
acknowledgements
related rules