

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Invio di messaggi di posta elettronica tramite Database Mail
<a name="SQLServer.DBMail.Send"></a>

Per inviare messaggi di posta elettronica utilizzando Database Mail, puoi utilizzare la stored procedure [sp\_send\_dbmail](https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-send-dbmail-transact-sql). 

## Utilizzo
<a name="SQLServer.DBMail.Send.Usage"></a>

```
EXEC msdb.dbo.sp_send_dbmail
@profile_name = '{{profile_name}}',
@recipients = '{{recipient1@example.com}}[; {{recipient2}}; ... {{recipientn}}]',
@subject = '{{subject}}',
@body = '{{message_body}}',
[@body_format = 'HTML'],
[@file_attachments = '{{file_path1}}; {{file_path2}}; ... {{file_pathn}}'],
[@query = '{{SQL_query'}}],
[@attach_query_result_as_file = {{0|1}}]';
```

I parametri seguenti sono obbligatori:
+ `@profile_name` – Il nome del profilo Database Mail da cui inviare il messaggio.
+ `@recipients` – L’elenco delimitato da punto e virgola di indirizzi di posta elettronica a cui inviare il messaggio.
+ `@subject` – L'oggetto del messaggio.
+ `@body` – Il corpo del messaggio. Puoi inoltre utilizzare una variabile dichiarata come corpo.

I parametri seguenti sono facoltativi:
+ `@body_format` – Questo parametro viene utilizzato con una variabile dichiarata per inviare e-mail in formato HTML.
+ `@file_attachments` – L’elenco delimitato da punto e virgola degli allegati dei messaggi. I percorsi dei file devono essere percorsi assoluti.
+ `@query` – Una query SQL da eseguire. I risultati della query possono essere allegati come file o inclusi nel corpo del messaggio.
+ `@attach_query_result_as_file` – Indica se allegare il risultato della query come file. Imposta su 0 per no, 1 per sì. Il valore predefinito è 0.

## Esempi
<a name="SQLServer.DBMail.Send.Examples"></a>

Negli esempi seguenti viene illustrato come inviare messaggi di posta elettronica.

**Example di invio di un messaggio a un singolo destinatario**  

```
USE msdb
GO

EXEC msdb.dbo.sp_send_dbmail
     @profile_name       = 'Notifications',
     @recipients         = 'nobody@example.com',
     @subject            = 'Automated DBMail message - 1',
     @body               = 'Database Mail configuration was successful.';
GO
```

**Example di invio di un messaggio a più destinatari**  

```
USE msdb
GO

EXEC msdb.dbo.sp_send_dbmail
     @profile_name       = 'Notifications',
     @recipients         = 'recipient1@example.com;recipient2@example.com',
     @subject            = 'Automated DBMail message - 2',
     @body               = 'This is a message.';
GO
```

**Example di invio di un risultato di una query SQL come file allegato**  

```
USE msdb
GO

EXEC msdb.dbo.sp_send_dbmail
     @profile_name       = 'Notifications',
     @recipients         = 'nobody@example.com',
     @subject            = 'Test SQL query',
     @body               = 'This is a SQL query test.',
     @query              = 'SELECT * FROM abc.dbo.test',
     @attach_query_result_as_file = 1;
GO
```

**Example di invio di un messaggio in formato HTML**  

```
USE msdb
GO

DECLARE @HTML_Body as NVARCHAR(500) = 'Hi, <h4> Heading </h4> </br> See the report. <b> Regards </b>';

EXEC msdb.dbo.sp_send_dbmail
     @profile_name       = 'Notifications',
     @recipients         = 'nobody@example.com',
     @subject            = 'Test HTML message',
     @body               = @HTML_Body,
     @body_format        = 'HTML';
GO
```

**Example di invio di un messaggio utilizzando un trigger quando si verifica un evento specifico nel database**  

```
USE AdventureWorks2017
GO
IF OBJECT_ID ('Production.iProductNotification', 'TR') IS NOT NULL
DROP TRIGGER Purchasing.iProductNotification
GO

CREATE TRIGGER iProductNotification ON Production.Product
   FOR INSERT
   AS
   DECLARE @ProductInformation nvarchar(255);
   SELECT
   @ProductInformation = 'A new product, ' + Name + ', is now available for $' + CAST(StandardCost AS nvarchar(20)) + '!'
   FROM INSERTED i;

EXEC msdb.dbo.sp_send_dbmail
     @profile_name       = 'Notifications',
     @recipients         = 'nobody@example.com',
     @subject            = 'New product information',
     @body               = @ProductInformation;
GO
```