lunes, 18 de octubre de 2021

Set Mail SQL SERVER

 We can send an email using the SQL Server. Database mail configuration information is maintained in an MSDB database. It is supporting logging and auditing features, using system tables of MSDB. We can send mail as a text message, HTML, query results, and files as an attachment. We have to follow some simple steps to achieve this.

Step 1. Go to Object Explorer.

Step 2. Expand the management menu, as shown below:

menu

Step 3. Right-click on database mail and select configure database mail, as shown below.

Configure Database Mail

After selecting “Configure Database Mail”, we will get the screenshot as shown below:

Configure Database Mail

Step 4. Click the Next button and after clicking the next button; we will get a new screenshot, as shown below.

Next

Step 5. Select the Radio button on the first option “Set up Database Mail by performing the following tasks” and click the Next button.

We will get a new Screen for setting up the account details for configuring the mail.

Step 6. Enter "Profile name" and "Description", as shown below:

Profile

Step 7. Click ADD button and we will get a new prompt where we can add more details related to the mail setup, as shown below:

  • Account name Enter the name of your new account.
  • Description Enter a description for the account. It is optional.
  • E-mail address Enter your e-mail address, which we will use for sending an e-mail, here you can specify your domain email id also as email@yourDomain.com.
  • Display name Enter the name which will use for displaying the name of the sender and it is optional.
  • Reply e-mail Enter the reply e-mail address, which will use for replies to e-mail messages sent from this account. It is also optional.
  • Server name Enter the IP address of the SMTP server for your e-mail account.
    This server requires a secure connection (SSL) - checked or unchecked as per your e-mail Domain.
  • SMTP Authentication We have to choose one Authentication type among three Authentication types.
    Here, I am using my Gmail account credentials to configure the mail setup. In most cases, we are using a company account.

    new

Step 8. Click OK. This screen will close and the previous screen is shown below.

new

Step 9. Click Next and we will get the prompt.

Step 10. Check the checkbox on “TestMailProfile” and make it the default profile, as shown below.

TestMailProfile

Step 11. Click Next and we will get a new screen.

Step 12. Keep the default setting for the system parameters and click the Next button, as shown below:

default setting

Step 13. Click the Finish button to complete the configuration, as shown below.

Finish

Step 14. It will do all the configurations and then click the close button.

configurations

Now, we are done with the mail configuration. We will test this to send a sample mail, with the help of the following steps.

Step 1. Go to Object Explore

Step 2. Expand the “Management” menu

Step 3. Right Click “Database Mail”

Step 4. Click “send Test E-Mail”, as shown below.

send Test E-Mail

After clicking “send Test E-Mail”, we will get a new screen.

Step 5. Database Mail Profile: Select “TestMailProfile”, as we created just now.

  • To Enter an e-mail Id of the receiver
  • Subject Enter the subject of your e-mail.
  • Body Enter the content of your mail.

Click the “Send Test E-mail” button, as shown below.

Send Test E-mail

An email will be sent to the recipient successfully.

After successfully configuring the Email in the SQL server, we will see how to send Email programmatically, with the help of a system procedure.

We are using the system procedure “sp_send_dbmail” to send an E-mail.

We can see the “sp_send_dbmail” system procedure by using “sp_helptext sp_send_dbmail”

The query will be written as shown below.

Query

We will send the parameters to the “sp_send_dbmail” system procedure, as per our requirement.

Here, I am using the parameters shown below to send an E-mail. 

use msdb
go
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'TestMailProfile',
@recipients = '',
@subject = 'DataBase Mail Test',
@body = 'This is a test e-mail.';
SQL

Explanation

  • Profile name We have to write the profile name which we created now.
  • Recipients we have to write the recipient's email. We can write multiple recipients' e-mail id by separating with ‘;’
  • Subject  We have to write the subject of the e-mail.
  • Body We have to write the body of the e-mail

We can also verify our E-mail status, whether it will successfully send or not, and get other information using the query given below:

use msdb
go
select * from sysmail_allitems
SQL

query

We can also see the database mail log, as shown below.

Mail Log

After clicking “View database Mail Log”, we will get the information about the database mail log, as shown below.

View database Mail Log







https://www.c-sharpcorner.com/article/configure-database-mail-send-email-from-sql-server-database/

https://www.c-sharpcorner.com/article/configure-database-mail-send-email-from-sql-server-database/















No hay comentarios:

Publicar un comentario