SQL Database Mail vs sp_send_dbmail issue

I have setup database mail in SQL 2008 and I received the test message in my inbox.

Headers from SSMS test message
Received: from mail.domainX.com ( by kvs-exch.domainX.com
 ( with Microsoft SMTP Server (TLS) id 8.1.436.0; Wed, 6 Aug 2014
 15:24:55 -0500
Received: from SQLDB ( by mail.domainX.com ( with
 Microsoft SMTP Server id 8.1.436.0; Wed, 6 Aug 2014 15:24:55 -0500
Sensitivity: Normal
Importance: Normal
MIME-Version: 1.0
From: SQLDB Server <sqldb@domainX.com>
To: <userX@domainX.com>
Date: Wed, 6 Aug 2014 15:24:55 -0500
Subject: Database Mail Test
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
Message-ID: <3575c90f-cf53-408d-9691-2808825a50cd@kvs-edge.domainX.com>
Return-Path: sqldb@domainX.com
X-MS-Exchange-Organization-PRD: domainX.com
Received-SPF: None (kvs-edge.domainX.com: sqldb@domainX.com does not
 designate permitted sender hosts)
X-MS-Exchange-Organization-PCL: 2
X-MS-Exchange-Organization-Antispam-Report: DV:3.3.14006.476;SV:3.3.8330.61;SID:SenderIDStatus None;OrigIP:
X-MS-Exchange-Organization-SCL: 1
X-MS-Exchange-Organization-SenderIdResult: NONE

Emails from trigger are treated as spam.  The code is sending email direct to the exchange server (not the SMTP server above).

EXEC msdb.dbo.sp_send_dbmail
@recipients = 'userx@domainx.com',
@subject = 'New user added',
@body = @msg

Headers from code above
Received: from kvs-exch.domainX.com ([]) by kvs-exch.domainX.com
 ([]) with mapi; Wed, 6 Aug 2014 15:26:32 -0500
Content-Type: application/ms-tnef; name="winmail.dat"
Content-Transfer-Encoding: binary
From: <sqldb@domainX.com>
To: User X <userX@domainX.com>
Date: Wed, 6 Aug 2014 15:26:31 -0500
Subject: Comments have been updated
Thread-Topic: Comments have been updated
Thread-Index: Ac+xs84GQuCaHt1ZRP6EUTZnT/sSCA==
Message-ID: <C9CBCDDAE7AC994EBF8E03620829739CCDCA78A9D9@kvs-exch.domainX.com>
Accept-Language: en-US
Content-Language: en-US
X-MS-Exchange-Organization-SenderIdResult: None
X-MS-Exchange-Organization-SCL: -1
X-MS-Exchange-Organization-PCL: 2
X-MS-Exchange-Organization-PRD: domainX.com
X-MS-TNEF-Correlator: <C9CBCDDAE7AC994EBF8E03620829739CCDCA78A9D9@kvs-exch.domainX.com>
received-spf: None (kvs-edge.domainX.com: sqldb@domainX.com does not
 designate permitted sender hosts)
MIME-Version: 1.0

Why doesn't sp_send_dbmail send email to the same server as the test message from SSMS?  I only have the 1 profile.
I expect sp_send_dbmail to send email to the server setup in the profile and it isn't do this.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nemws1Database AdministratorCommented:
You mean the e-mail server associated with the *account* - not  the profile.  MSSQL Profiles do not specify e-mail servers, but MSSQL Accounts in DB mail *do*.

Double-check your settings here:
USE msdb;
FROM sysmail_server;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hbaberAuthor Commented:
Their is the offending entry!  Why is it using the incorrect server thou when I tell it which server to use in the profile?
nemws1Database AdministratorCommented:
I find SQL's e-mail configuration to be overly complex.

In SSMS, connect to the database server in question and expand out the "Management" tab (this is the object explorer).  Double click on "Database Mail".  Then click on "Manage Database Mail accounts and profiles" and click "Next".  Then select "View, change, or delete an existing account" and click "Next".

Select any accounts you are using via the dropdown selection beside "Account name".  In each account, you will see an entry for "Server name:" below.  Find and correct the errant entry and click "Finish" at the bottom.

Hopefully that will get your e-mail around your spam filter. :)
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

hbaberAuthor Commented:
I am still at a lost why the spam filter blocks the code generated email but not the test email from SSMS.

Thanks for the help.
hbaberAuthor Commented:
Removing the wrong entry has broken email now.
now I get this error....
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2014-08-07T13:39:17). Exception Message: Could not connect to mail server. (No such host is known). )
nemws1Database AdministratorCommented:
Did you remove the entry or fix it?  I would have just updated the e-mail server to the correct servername.
hbaberAuthor Commented:
I removed it :(
nemws1Database AdministratorCommented:
Well, your profile is most likely still pointing to that account.  Try creating a new account with the proper addresses and e-mail server and attach that to the profile you're using in your msdb.dbo.sp_send_dbmail call (you have 'EmailSERVER' above).
hbaberAuthor Commented:

The section "Important Tables used in configuring Database mail and check their status" helped me clean up everything and recreated account/profile using wizard, added IP bypass to spam filter and email is working now.
nemws1Database AdministratorCommented:
Awesome news. :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.