{"id":9809,"date":"2020-09-03T09:55:10","date_gmt":"2020-09-02T23:55:10","guid":{"rendered":"https:\/\/database.guide\/?p=9809"},"modified":"2020-09-11T08:40:51","modified_gmt":"2020-09-10T22:40:51","slug":"create-a-database-mail-profile-in-sql-server-t-sql","status":"publish","type":"post","link":"https:\/\/database.guide\/create-a-database-mail-profile-in-sql-server-t-sql\/","title":{"rendered":"Create a Database Mail Profile in SQL Server (T-SQL)"},"content":{"rendered":"\n<p>When you use Database Mail in <a href=\"https:\/\/database.guide\/what-is-sql-server\/\" title=\"What is SQL Server?\">SQL Server<\/a> to <a href=\"https:\/\/database.guide\/how-to-send-emails-from-sql-server-t-sql\/\" title=\"How to Send Emails from SQL Server (T-SQL)\">send emails<\/a>, you can specify a Database Mail profile to send the mail from. <\/p>\n\n\n\n<p>In some cases you will be required to specify a profile when you send the email. This is the case when the user does not have a default private profile and there is no default public profile for the database.<\/p>\n\n\n\n<p>You can create Database Mail profiles with <a href=\"https:\/\/database.guide\/what-is-t-sql\/\" title=\"What is T-SQL?\">T-SQL<\/a> by executing the <code>sysmail_add_profile_sp<\/code> stored procedure in the <code>msdb<\/code> database. <\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s an example of creating a Database Mail profile.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXECUTE msdb.dbo.sysmail_add_profile_sp  \n    @profile_name = 'DB Admin Profile',  \n    @description = 'Profile for admin emails.';<\/code><\/pre>\n\n\n\n<p>In this example I specify a profile name of &#8220;DB Admin Profile&#8221;, and I provide a description. <\/p>\n\n\n\n<p>You also have the option of providing a third argument to return the ID of the new profile (see syntax below).<\/p>\n\n\n\n<p>Note that the <code>sysmail_add_profile_sp<\/code> stored procedure is in the <code>msdb<\/code> database, and it&#8217;s owned by the <code>dbo<\/code> schema. Therefore, you&#8217;ll need to use three part naming if you execute it from outside the <code>msdb<\/code> database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Syntax<\/h2>\n\n\n\n<p>The official syntax goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sysmail_add_profile_sp &#91; @profile_name = ] 'profile_name'  \n    &#91; , &#91; @description = ] 'description' ]  \n    &#91; , &#91; @profile_id = ] new_profile_id OUTPUT ]<\/code><\/pre>\n\n\n\n<p>See the <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/system-stored-procedures\/sysmail-add-profile-sp-transact-sql\" target=\"_blank\" rel=\"noreferrer noopener\">Microsoft documentation<\/a> for a detailed explanation of each argument. <\/p>\n\n\n\n<p>You can also use the <code>sysmail_update_profile_sp<\/code> stored procedure to <a href=\"https:\/\/database.guide\/update-a-database-mail-profile-in-sql-server-t-sql\/\" title=\"Update a Database Mail Profile in SQL Server (T-SQL)\">update an existing Database Mail profile<\/a>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configuring Database Mail<\/h2>\n\n\n\n<p>Creating a profile is just one of several things you need to do before you can send email with that profile. <\/p>\n\n\n\n<p>Once you&#8217;ve created the profile, you&#8217;ll need to <a href=\"https:\/\/database.guide\/add-a-database-mail-account-to-a-profile-t-sql\/\" title=\"Add a Database Mail Account to a Profile (T-SQL)\">add at least one Database Mail account to that profile<\/a> (which means that you first need to <a href=\"https:\/\/database.guide\/create-a-database-mail-account-in-sql-server-t-sql\/\" title=\"Create a Database Mail Account in SQL Server (T-SQL)\">create an account<\/a>, if it doesn&#8217;t exist already). You&#8217;ll also need to <a href=\"https:\/\/database.guide\/grant-an-msdb-user-access-to-a-database-mail-profile-in-sql-server-t-sql\/\">grant a user in the <strong>msdb<\/strong> database access to the profile<\/a>.<\/p>\n\n\n\n<p>See <a href=\"https:\/\/database.guide\/how-to-send-emails-from-sql-server-t-sql\/\" title=\"How to Send Emails from SQL Server (T-SQL)\">How to Send Email in SQL Server<\/a> for a complete example of configuring Database Mail, then sending an email.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you use Database Mail in SQL Server to send emails, you can specify a Database Mail profile to send the mail from. In some cases you will be required to specify a profile when you send the email. This is the case when the user does not have a default private profile and there &#8230; <a title=\"Create a Database Mail Profile in SQL Server (T-SQL)\" class=\"read-more\" href=\"https:\/\/database.guide\/create-a-database-mail-profile-in-sql-server-t-sql\/\" aria-label=\"Read more about Create a Database Mail Profile in SQL Server (T-SQL)\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[10,77,104,61],"class_list":["post-9809","post","type-post","status-publish","format-standard","hentry","category-sql-server","tag-how-to","tag-mssql","tag-send-email","tag-t-sql"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/9809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=9809"}],"version-history":[{"count":7,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/9809\/revisions"}],"predecessor-version":[{"id":9940,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/9809\/revisions\/9940"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=9809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=9809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=9809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}