How to configure database smtp mail in sql server
To configure database SMTP mail in SQL Server, you'll need to follow these steps:
Step 1: Enable Database Mail
- Open SQL Server Management Studio (SSMS) and connect to your SQL Server instance.
- In the Object Explorer, expand the "SQL Server" node, then right-click on "SQL Server" and select "Properties".
- In the "SQL Server Properties" window, click on the "Database Settings" tab.
- Under "Database Mail", click on the "Enable Database Mail" checkbox.
- Click "OK" to save the changes.
Step 2: Configure Database Mail Profile
- In the Object Explorer, expand the "SQL Server" node, then right-click on "SQL Server" and select "Database Mail".
- In the "Database Mail" window, click on the "New Mail Profile" button.
- In the "New Mail Profile" window, enter a name for your mail profile (e.g., "MyMailProfile").
- Enter the SMTP server name, port number, and authentication details (e.g., username and password).
- Click "OK" to save the changes.
Step 3: Configure Database Mail Account
- In the Object Explorer, expand the "SQL Server" node, then right-click on "SQL Server" and select "Database Mail".
- In the "Database Mail" window, click on the "New Mail Account" button.
- In the "New Mail Account" window, select the mail profile you created in Step 2.
- Enter the email address and display name for the mail account.
- Click "OK" to save the changes.
Step 4: Test Database Mail
- In the Object Explorer, expand the "SQL Server" node, then right-click on "SQL Server" and select "Database Mail".
- In the "Database Mail" window, click on the "Send Test Mail" button.
- If the test mail is sent successfully, you should receive an email at the email address you specified in Step 3.
Step 5: Configure SQL Server Agent to Use Database Mail
- In the Object Explorer, expand the "SQL Server" node, then right-click on "SQL Server Agent" and select "Properties".
- In the "SQL Server Agent Properties" window, click on the "Alert System" tab.
- Under "Database Mail", select the mail profile you created in Step 2.
- Click "OK" to save the changes.
Now, you should be able to use Database Mail to send emails from your SQL Server instance. You can use the sp_send_dbmail
stored procedure to send emails programmatically.
Here's an example of how to use sp_send_dbmail
:
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'MyMailProfile',
@recipients = '[email protected]',
@subject = 'Test Email',
@body = 'This is a test email sent from SQL Server.'
Replace MyMailProfile
with the name of your mail profile, and [email protected]
with the email address of the recipient.