This article introduces how to use the SQL Server Stored Procedure sp_send_dbmail to send emails.
1) create a configuration file and account (create a configuration file and configure database mail Wizard to access the database mail node in the configuration database mail management node and the account Used in the context menu .)
The configuration file name is used as a parameter when sp_send_dbmail is used.
Click "add"
Here, the account name can be specified (the description function is enough). The focus is on the configuration of the mail sending server (SMTP): the e-mail address is the sender's mail address, and the display name is the sender's name, reply to the email (which can be the same as the email address). The server name is the port number of the server sending server (smtp.exmail.qq.com For eg: QQ). SMTP authentication: basic Authentication (configure your own mail sender address and password ).
You can add multiple sender accounts. If one account fails, use another account to send messages.
Click "Next"
2) Configure email
-- You can manually use SQL code to enable the database mail function (if you do not need SQL CODE to enable the database mail function, in the process of configuring the database mail wizard, the system will prompt "whether to enable the database email function". Select "yes)
Exec sp_configure 'show advanced options', 1
Reconfigure
Go
Exec sp_configure 'database mail xps ', 1
Reconfigure
Go
/*
Database Mail retains copies of the sent emails. We can query them through sysmail_allitems, sysmail_sentitems, sysmail_unsentitems, and sysmail_faileditems. The mail sending status will be saved in the sysmail_mailitems table. When the mail is sent successfully, the sent_status column in this table will be displayed as 1; when the mail fails to be sent, the sent_status column will be displayed as 2, and the mail sending status will be 3
As shown below, log files can be viewed in the sysmail_log table
*/
3) send an email.
-- Or execute the stored procedure in the MSDB Database
EXEC sp_send_dbmail @ profile_name = 'db _ mail_test ',
@ Recipients = 'test @ Example.com ',
@ Subject = 'test message ',
@ Body = 'test whether the database email is sent successfully .'
Click "send ......"
Finally, go to the mailbox or view the mail log for verification.