There are various reasons for this error code when trying to send emails from SQL Server. Most of the resolution folks have posted are related to security and permissions.
That was not in my case, i had to do numerous iterations to get the sequence of the parameters right. Below is how i sequenced the parameters for the procedure:
EXEC msdb.dbo.sp_send_dbmail @profile_name = 'DB_MAIL',
@recipients = @Recipients,
@subject = @Subject,
@body = @body,
@execute_query_database = 'mydb',
@query_result_separator = @tab,
@query_result_header = 1,
@query_result_width = 32767,
@query_result_no_padding = 1,
@attach_query_result_as_file = 1,
@query_attachment_filename = @FileName,
@query = @QueryTxt;