How to send database mail in sql server 2012
In SQL Server 2012, you can send database mail using the sp_send_dbmail
stored procedure. Here are the steps to send a database mail:
Step 1: Configure Database Mail
Before sending a database mail, you need to configure the Database Mail feature. To do this:
- Open SQL Server Management Studio (SSMS) and connect to your SQL Server instance.
- Expand the "Management" folder in the Object Explorer.
- Right-click on "Database Mail" and select "Configure Database Mail".
- Follow the wizard to configure the Database Mail feature.
Step 2: Create a Profile
A profile is a set of settings that define how the mail should be sent. To create a profile:
- In the "Database Mail" configuration window, click on the "New Profile" button.
- Enter a name for the profile and select the mail server type (e.g., SMTP).
- Enter the mail server settings, such as the server name, port, and authentication settings.
- Click "OK" to create the profile.
Step 3: Send a Database Mail
To send a database mail, use the sp_send_dbmail
stored procedure. The syntax is as follows:
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Your_Profile_Name',
@recipients = '[email protected]',
@subject = 'Your Subject',
@body = 'Your message body'
Replace Your_Profile_Name
with the name of the profile you created in Step 2, [email protected]
with the email address of the recipient, Your Subject
with the subject of the email, and Your message body
with the body of the email.
Example:
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'My Mail Profile',
@recipients = '[email protected]',
@subject = 'Database Mail Test',
@body = 'This is a test email sent from SQL Server 2012.'
Note: Make sure to replace the @profile_name
parameter with the actual name of the profile you created in Step 2.
That's it! The database mail should be sent successfully. You can check the mail logs in the msdb.dbo.sysmail_log
system table to verify the status of the mail.