Fix it now
Error 22050 comes out of sp_send_dbmail itself, so the mail server is usually not involved. The message text varies and is the diagnosis: a query that failed, a caller who cannot use the profile, or a pair of formatting parameters that cannot be combined. Error 15281 is different again and means the component is switched off.
EXECUTE sp_configure 'show advanced options', 1;
RECONFIGURE;
EXECUTE sp_configure 'Database Mail XPs', 1;
RECONFIGURE;
SELECT TOP 50 log_id, event_type, log_date, description
FROM msdb.dbo.sysmail_event_log ORDER BY log_date DESC;
- Read the whole 22050 message, not just the number. Microsoft’s own examples include “Failed to initialize sqlcmd library with error number …” and “Failed to execute the query because the @query_result_no_append and @query_no_truncate options are mutually exclusive”, which are parameter problems rather than mail problems.
- If your call uses
@query, run that query by hand in the database you pass as@execute_query_database, as the account the job runs under. The query executes on a separate connection, so nothing from your calling batch exists there. - Confirm the caller is a member of
DatabaseMailUserRolein msdb and has permission on the profile. Without permission on the profile, sp_send_dbmail returns an error and sends nothing. - Send a test message from SQL Server Management Studio, under Management, Database Mail, Send Test E-Mail, to prove basic delivery separately from your call.
Enabling Database Mail XPs takes effect immediately; no restart is required.
If your call now returns cleanly and the mail arrives, stop here. If messages queue but never arrive, the next sections separate the two halves of Database Mail.
Why it happens
Database Mail is two things joined by a queue. The procedure sp_send_dbmail validates the request, queues the message in msdb and returns; delivery happens afterwards, in a separate external process that reads the queue and talks to your mail server. That split is the single most useful thing to know here, because a failure at the moment of the call means the message never reached the queue, so SMTP cannot be the explanation.
Error 22050 is what that validation failure looks like. It has no fixed text: it carries whatever went wrong, which is why two people can compare 22050 errors and find they have nothing in common. Microsoft’s reference for the procedure quotes two examples verbatim, both caused by parameter combinations rather than by mail. It is also worth knowing that 22050 does not appear in Microsoft’s published message list for the 22,000 range, so there is no canonical sentence to look up.
If you supply a query it is executed on a separate connection before anything is queued. It runs in the database given by @execute_query_database, and it has a documented restriction that catches people out: the principal executing sp_send_dbmail must be connected as an individual login rather than as a member of a Windows or Microsoft Entra group. Database Mail itself runs in the security context of the SQL Server service account, which is why a SQL-authenticated user cannot use @file_attachments.
Formatting is the third place things break, and the parameters interact. @query_result_width defaults to 256 characters and accepts 10 to 32767. @query_no_truncate stops large variable-length values being cut to 256 characters, but setting it also removes the column headers. @query_result_no_padding overrides the width and is mutually exclusive with @query_no_truncate. Combine the wrong two and the call fails with 22050 before a single byte is queued.
The component is switched off
You have this one if Error 15281 rather than 22050, raised the moment the procedure is called, usually on a freshly built or hardened instance.
- Enable it with the
sp_configuresequence above, which needs advanced options shown first. - Send a test message to confirm; the setting takes effect immediately without a restart.
- If a hardening baseline turns it off again, get an exception for instances that must send mail rather than re-enabling it every month.
The query supplied to the procedure fails
You have this one if Mail works without @query and fails with it, and the log entry describes an execution problem.
- Run the query directly in the database you pass as
@execute_query_database, logged in as the account the job uses. - Qualify object names in full. The query runs in its own session and may not have the database context you assumed.
- Check that the caller is an individual login rather than a group member, which is a documented requirement for
@query.
Error 14661 is published as “Query execution failed” followed by the message. If you see it in the mail log, the query is the problem and nothing downstream is.
The caller cannot use a profile
You have this one if The same call succeeds for a sysadmin and fails for an application login or a job owner.
- Add the login as a user in msdb and put it in the
DatabaseMailUserRoledatabase role. - Grant that principal access to the profile, or name the profile explicitly with
@profile_name. - If nothing is named and nothing is configured, expect error 14636, published as “No global profile is configured. Specify a profile name in the @profile_name parameter.”
The formatting parameters conflict
You have this one if The call fails immediately with a 22050 whose text mentions sqlcmd or names two parameters.
- Remove
@query_no_truncateif you also set@query_result_headerto 0, which is one of the documented failing combinations. - Choose between
@query_no_truncateand@query_result_no_padding; they are documented as mutually exclusive. - For anything sizeable use
@attach_query_result_as_file = 1so the result travels as an attachment instead of being formatted into the body.
Messages are accepted but never delivered
You have this one if The call succeeds, nothing arrives, and failed items accumulate.
- Read
msdb.dbo.sysmail_faileditemsandmsdb.dbo.sysmail_unsentitemsto see whether the message is failing or still retrying. - Check the queue status and start it if it is stopped.
- Only now look at the account settings: server name, port, whether SSL is required, and whether your mail server accepts relay from this host.
Full reference
The tables that answer each question
| Object in msdb | What it tells you |
|---|---|
sysmail_event_log |
Every Windows and SQL Server message Database Mail logged, newest last |
sysmail_faileditems |
Messages that were queued and then failed to send |
sysmail_unsentitems |
Messages still queued or retrying |
sysmail_sentitems |
Messages Database Mail believes it delivered |
Parameters that change whether the call succeeds at all
| Parameter | Default | What to know |
|---|---|---|
@profile_name |
None named | Falls back to your default private profile, then the msdb public default. With neither, you get 14636 |
@execute_query_database |
Current database | Where @query runs. Not where you called from |
@query_result_width |
256 | Line width in characters, valid 10 to 32767 |
@query_no_truncate |
0 | Stops large values being cut to 256 characters, and removes column headers |
@query_result_no_padding |
0 | Overrides the width; mutually exclusive with @query_no_truncate |
@attach_query_result_as_file |
0 | 1 sends the result as an attachment instead of in the body |
@append_query_error |
0 | 1 sends the mail with the query error in the body; 0 sends nothing and returns 1 |
Permissions, in the order they are checked
- The caller must be able to execute
sp_send_dbmail, which defaults to members ofDatabaseMailUserRolein msdb. - The caller must have permission to use the profile. Without it, the procedure returns an error and does not send the message.
- For
@query, the caller must be connected as an individual login rather than through a group membership. - The query itself then runs with whatever rights that principal has in
@execute_query_database.
Why Agent notifications can fail while mail works
Testing mail from Management Studio proves Database Mail works. It does not prove that SQL Server Agent has been told to use it. Agent has its own alert system configuration naming the profile it will use, and it has to be restarted after that is set. If test mail arrives and job notifications do not, look there before you look at Database Mail at all.
Housekeeping
- Everything sent, failed or queued stays in the msdb mail tables until you remove it, and those tables grow indefinitely.
- Include a purge step in your maintenance jobs, and keep enough history to investigate a bad week.
- Watch msdb’s size if mail volumes are high, because this is one of the commoner reasons for it to grow quietly.
- SQL Server Express includes neither SQL Server Agent nor Database Mail, so on Express the answer is a different notification route rather than a setting.
Every code this article covers
| Code | What it points at | Source |
|---|---|---|
22050 |
Raised by sp_send_dbmail when the request could not be completed; the text varies and is the diagnosis, commonly a failed query or a conflicting pair of formatting parameters | Microsoft Learn |
15281 |
SQL Server blocked access to the component because it is turned off as part of the security configuration for this server; enable it with sp_configure | Microsoft Learn |
14661 |
Query execution failed, followed by the error the query itself raised | Microsoft Learn |
14636 |
No global profile is configured; specify a profile name in the @profile_name parameter | Microsoft Learn |
Confirm the fix worked
- A test message from Management Studio arrives.
- The original
sp_send_dbmailcall, including its query, returns without error. sysmail_faileditemsgains no new rows after the change.sysmail_event_logshows the send rather than an error for that message.- The scheduled job that failed runs once on its own and the mail arrives.
Questions people ask about this
Do I need to change anything on the mail server?
Usually not for 22050, because the message never reached the mail server. Once messages queue successfully and still fail to arrive, the account settings and your mail server’s relay rules are the right place to look.
Does fixing this require a licence or a different edition?
No, and it costs nothing. The one thing worth knowing is that SQL Server Express includes neither Agent nor Database Mail, so on Express this is not a setting to change but a different way of sending the notification.
Why does my query work in a query window and fail from sp_send_dbmail?
Because it runs on a separate connection, in the database given by @execute_query_database, with none of your session’s temporary objects or variables. There is also a documented restriction that the caller must be connected as an individual login rather than as a member of a group.
Do I have to restart SQL Server after enabling Database Mail XPs?
No. Microsoft documents the setting as taking effect immediately without a server stop and restart.
My results arrive with the columns cut short. Is that an error?
No, it is the default formatting. Results are formatted to a line width that defaults to 256 characters, and large values are truncated at 256 unless you set @query_no_truncate. Setting that also removes the column headers, and it cannot be combined with @query_result_no_padding.
