Skip to main content

SQL Server Database Mail setup

Firstly we need to enable Database Mail feature in the server that has MS SQL Server installed. This can be done using GUI or TSQL. In this tutorial I will be demonstrating using TSQL, as I prefer it.

Step 01:
Enable Database Mail feature in MS SQL.

USE master
go
sp_configure 'show advanced options',1
go
reconfigure with override
go
sp_configure 'Database Mail XPs',1
go
reconfigure
go

Step 02:
Create database account.

EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = 'Admin',
@description = 'Mail account for Database Mail',
@email_address = 'me@example.com',
@display_name = 'Younten Jamtsho',
@username='me@example.com',
@password='meeeeeeeeeeee',
@mailserver_name = 'mail.example.com'

Step 03:
Create a mail profile.

EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = 'AdminProfile',
@description = 'Profile used for database mail'

Step 04:
Add the account to the profile.

EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = 'AdminProfile',
@account_name = 'Admin',
@sequence_number = 1

Step 05:
Grant access to the profile.

EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
@profile_name = 'AdminProfile',
@principal_name = 'public',
@is_default = 1;

Now that you have configured the Database Mail. Lets sending mail though it.

EXEC msdb.dbo.sp_send_dbmail @recipients='you@example',
@subject = 'Database Mail',
@body = 'Testing mail system of SQL Server',
@profile_name = 'AdminProfile'


If in any case you run into some problem and you need to delete all the configurations that you have just done. Here is how to do it.

EXECUTE msdb.dbo.sysmail_delete_profileaccount_sp
@profile_name = 'AdminProfile',
@account_name = 'Admin'

EXECUTE msdb.dbo.sysmail_delete_profile_sp
@profile_name = 'AdminProfile'

EXECUTE msdb.dbo.sysmail_delete_account_sp
@account_name = 'Admin'

Here another way you can delete any Database Mail account and profile in your SQL Server.

Declare @ProfileName nvarchar(20), @AccountName nvarchar(20)
IF EXISTS(
SELECT * FROM msdb.dbo.sysmail_profileaccount pa
JOIN msdb.dbo.sysmail_profile p ON pa.profile_id = p.profile_id
JOIN msdb.dbo.sysmail_account a ON pa.account_id = a.account_id
WHERE
p.name = @ProfileName AND
a.name = @AccountName)
BEGIN
PRINT 'Deleting Profile Account'
EXECUTE msdb.dbo.sysmail_delete_profileaccount_sp
@profile_name = @ProfileName,
@account_name = @AccountName
END

IF EXISTS(
SELECT * FROM msdb.dbo.sysmail_profile p
WHERE p.name = @ProfileName)
BEGIN
PRINT 'Deleting Profile.'
EXECUTE msdb.dbo.sysmail_delete_profile_sp
@profile_name = @ProfileName
END

IF EXISTS(
SELECT * FROM msdb.dbo.sysmail_account a
WHERE a.name = @AccountName)
BEGIN
PRINT 'Deleting Account.'
EXECUTE msdb.dbo.sysmail_delete_account_sp
@account_name = @AccountName
END

Here are the list of tables associated with Database Mail in SQL Server.

--Profiles
SELECT * FROM msdb.dbo.sysmail_profile

--Accounts
SELECT * FROM msdb.dbo.sysmail_account

--Profile Accounts
select * from msdb.dbo.sysmail_profileaccount

--Principal Profile
select * from msdb.dbo.sysmail_principalprofile

--Mail Server
SELECT * FROM msdb.dbo.sysmail_server
SELECT * FROM msdb.dbo.sysmail_servertype
SELECT * FROM msdb.dbo.sysmail_configuration

--Email Sent Status
SELECT * FROM msdb.dbo.sysmail_allitems
SELECT * FROM msdb.dbo.sysmail_sentitems
SELECT * FROM msdb.dbo.sysmail_unsentitems
SELECT * FROM msdb.dbo.sysmail_faileditems

--Email Status
SELECT SUBSTRING(fail.subject,1,25) AS 'Subject',
fail.mailitem_id,
LOG.description
FROM msdb.dbo.sysmail_event_log LOG
join msdb.dbo.sysmail_faileditems fail
ON fail.mailitem_id = LOG.mailitem_id
WHERE event_type = 'error'

--Mail Queues
EXEC msdb.dbo.sysmail_help_queue_sp

--DB Mail Status
EXEC msdb.dbo.sysmail_help_status_sp

Comments

Popular posts from this blog

Clear Zimbra Mail Queue

If you want to clear mail queue from specific email address run the following command in your terminal. Below will remove mail queues, by checking FROM sender. eg. remove all mail queues sent by info@example.com /opt/zimbra/postfix/sbin/mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { if ($7 == "info@example.comt") print $1 } ' | tr -d '*!' | xargs -rn1 /opt/zimbra/postfix/sbin/postsuper -d Below will remove mail queues, by checking TO sender. eg. remove all mail queues sent to info@example.com /opt/zimbra/postfix/sbin/mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { if ($8 == "info@example.comt") print $1 } ' | tr -d '*!' | xargs -rn1 /opt/zimbra/postfix/sbin/postsuper -d

Zimbra Mail Error: not enough free space in mail queue

If you are using Zimbra Mail and you are not able to send any mails and also you see an Error Message " not enough free space in mail queue " under /var/log/message. Then you should try out this solution. 1. Login to the server, and then again login as zimbra as follows: #su - zimbra 2. Check the Postfix message_size_limit, by default it should be 10MB (i.e. '10240000' bytes) as follows: $postconf message_size_limit message_size_limit = 10240000 3. If the value is higher than that, set it as follows to 10MB $ zmprov modifyConfig zimbraMtaMaxMessageSize 10240000 $ postfix reload Now try sending Mail, It should work. You could try out this solution for Error: " SMTP Server Reported: 452 4.3.1 Insufficient System Storage "