Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Email through SQL Server FAILED

I am setting up Database mail in SQL Server 2008 to send mail to my Gmail account. Following are the details of the DBMail profile I've created:

email id : [email protected]
Display Name : xyz
Reply email :
Server name : smtp.gmail.com
port no: 587
Check : This server requires a secure connection
Check : Basic Authentication
username : [email protected] ( should have gmail.com)
password : <mypassword>
confirm password : <mypassword>

Then I clicked next and also made it default public profile.

Then I use the following TSQL code to configure and send DBMail:

EXEC sp_CONFIGURE 'show advanced', 1
GO
RECONFIGURE
GO
EXEC sp_CONFIGURE 'Database Mail XPs', 1
GO
RECONFIGURE
GO

use msdb
go
EXEC sp_send_dbmail 
@profile_name='XYZ',
@recipients='[email protected]',
@subject='Test message',
@body='Message Sent Successfully'

The output appears "Message Queued"

Still my message is getting failed. I feel I have setup the server correctly. Any input would be appreciated.

 SELECT * FROM sysmail_log

The body column shows

"The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2012-05-16T17:05:00). Exception Message: Could not connect to mail server. (No such host is known). )"

like image 241
user1141584 Avatar asked Feb 21 '23 11:02

user1141584


2 Answers

Some suggested troubleshooting steps:

  1. Try to send a message through the same SMTP server, using the same machine as SQL Server, using a client mail program like Outlook. You may get a more meaningful error message.

  2. The port may be blocked. Try sending mail to any other SMTP server using port 587.

  3. Try changing to port 465.

  4. Try unchecking the "requires a secure connection" bit in your profile.

  5. Try using an SMTP server inside your network to relay to GMail. This might be easier to set up and get running. You'll need to install IIS and the SMTP components if you don't have a local SMTP server available to handle the relay.

  6. Review http://mail.google.com/support/bin/answer.py?hl=en&answer=13287

like image 113
Aaron Bertrand Avatar answered May 03 '23 00:05

Aaron Bertrand


If you are using GMAIL then please take care of one extra step below:

  • After you configure the email in the Database Mail, Please log in your gmail account and go to

    https://www.google.com/settings/security/lesssecureapps

Turn on your less secure app to use your account, that's how the database mail can access your gmail SMTP account.

Try sending test mail using following code:

    USE msdb
    GO
    EXEC sp_send_dbmail @profile_name='XXX', -- Your profile name
    @recipients='[email protected]', -- Recipient's email address
    @subject='Test message',
    @body='This is the body of the test message.'

At last, try to run following query to see whether its sent or not

    SELECT * FROM sysmail_log
like image 29
Denn Avatar answered May 03 '23 00:05

Denn