5.7.1 SMTP error when running stored proc using DB Mail in SQL Server 2012

microsoft-office-365smtpsql-server-2012stored-procedures

I am getting an error in SQL Server 2012 Database Mail when executing a stored proc to send an email from a specified address to a specified address using Office 365 as the SMTP server.

The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2014-06-10T15:28:48). Exception Message: Cannot send mails to mail server. (Mailbox unavailable. The server response was: 5.7.1 Client does not have permissions to send as this sender).

This same stored proc is working in production on another server fine. The only change is that this instance of the working stored proc is SQL Server 2008 R2.

Judging from the error this is a SMTP error, am I correct?
All users I am using to send and receive the mail are in the same O365 domain and have {SendAs} access rights and NT Authority\Self Trustees in O365.

I can telnet office 365 over the specified port (587) fine and even stranger is I can send email using the 'Send Test E-Mail' feature in DB Mail fine using the same Office 365 profile that is specified in the stored proc.

Any help would be appreciated.

Screenshot of database mail settingsenter image description here

Best Answer

To fix this issue I have installed a SMTP server locally on the same server as the SQL Server instance. I configured DB mail to localhost then set the Office 365 settings in the SMTP server ensuring that TLs encryption and the correct port is stated.

This seemed to fix the issue for me.