Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Aktifkan Database Mail di SQL Server (T-SQL)

SQL Server memiliki opsi email database yang dapat Anda gunakan untuk mengirim email dari server database.

Misalnya, Anda bisa mendapatkan notifikasi saat tugas Agen Server SQL selesai berjalan atau gagal, atau saat ada kesalahan tingkat tinggi, dll.

Saat Database Mail Tidak Dikonfigurasi

Di SQL Server, email dikirim dengan menjalankan sp_send_dbmail prosedur tersimpan di msdb basis data.

Ini contohnya:

EXEC msdb.dbo.sp_send_dbmail  
    @profile_name = 'DB Admin Profile',  
    @recipients = '[email protected]',  
    @body = 'Your favorite SQL Server Agent job just failed',  
    @subject = 'SQL Server Agent Job: FAILED';

Namun, ini mengasumsikan bahwa SQL Server dikonfigurasi untuk mengirim email.

Jika Anda mencoba mengirim email dari SQL Server, tetapi Anda mendapatkan error seperti berikut, berarti Anda saat ini tidak mengaktifkan Database Mail.

Msg 15281, Level 16, State 1, Procedure msdb.dbo.sp_send_dbmail, Line 0
SQL Server blocked access to procedure 'dbo.sp_send_dbmail' of component 'Database Mail XPs' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Database Mail XPs' by using sp_configure. For more information about enabling 'Database Mail XPs', search for 'Database Mail XPs' in SQL Server Books Online.

Aktifkan Database Mail XPs

Sebelum Anda mulai mengirim email dari server, Anda perlu memastikan bahwa Anda telah mengaktifkan Database Mail XPs.

Ini cukup mudah dilakukan (walaupun, Microsoft menyarankan bahwa opsi lanjutan seperti ini harus diubah hanya oleh administrator database berpengalaman atau teknisi SQL Server bersertifikat).

Berikut cara mengaktifkan Database Mail XPs:

EXEC sp_configure 'show advanced options', '1';
RECONFIGURE
GO
EXEC sp_configure 'Database Mail XPs', 1;
RECONFIGURE
GO

Hasil:

Configuration option 'show advanced options' changed from 0 to 1. Run the RECONFIGURE statement to install.
Commands completed successfully.
Commands completed successfully.

Buat Akun Email, Profil, dll

Database Mail dikirim melalui profil, bukan akun pengguna secara langsung.

Untuk mengirim email dengan Database Mail, Anda perlu membuat akun Database Mail, profil Database Mail, menambahkan akun ke profil, lalu memberikan akses pengguna ke profil tersebut. Pengguna harus berada di msdb basis data.

Kode T-SQL untuk melakukan ini dapat terlihat seperti ini:

-- Switch to the msdb database
USE msdb;

-- Create a user on the msdb database
CREATE USER Marge FOR LOGIN Marge;

-- Create a Database Mail account  
EXECUTE msdb.dbo.sysmail_add_account_sp  
    @account_name = 'DB Admin',  
    @description = 'Mail account for admin emails.',  
    @email_address = '[email protected]',  
    @replyto_address = '[email protected]',  
    @display_name = 'DB Automated Mailer',  
    @mailserver_name = 'smtp.example.com',
    @port = 25;  
  
-- Create a Database Mail profile  
EXECUTE msdb.dbo.sysmail_add_profile_sp  
    @profile_name = 'DB Admin Profile',  
    @description = 'Profile for admin emails.';  
  
-- Add the account to the profile  
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp  
    @profile_name = 'DB Admin Profile',  
    @account_name = 'DB Admin',  
    @sequence_number = 1;
  
-- Grant the msdb user access to the Database Mail profile
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
    @profile_name = 'DB Admin Profile',
    @principal_name = 'Marge',
    @is_default = 1;

Anda harus mengganti berbagai detail dengan milik Anda sendiri. Ini juga mengasumsikan bahwa Anda menentukan server email yang berfungsi.

Setelah Anda selesai melakukannya, Anda seharusnya dapat mengirim email dengan msdb.dbo.sp_send_dbmail prosedur tersimpan.


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Bandingkan tanggal dalam T-SQL, abaikan bagian waktu

  2. Cara Membulatkan (ATAS/BAWAH) di SQL Server – 5 Tips Berguna

  3. Bagaimana cara menulis ulang IS BERBEDA DARI dan TIDAK BERBEDA DARI?

  4. Kode kerangka kerja entitas lambat saat menggunakan Include() berkali-kali

  5. Impor Excel SSIS Memaksa Jenis Kolom Salah