title: Unable to send an email from SQL Query in SQL Server 2012/2014/2016
body: <p>I configured database Mail from the wizard and able to get email alerts. </p>
  
  <p><a href="https://i.stack.imgur.com/IOf8x.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IOf8x.png" alt="enter image description here"></a></p>
  
  <p>When I'm writing the following query:</p>
  
  <pre><code>DECLARE @IsFileExists INT   
  EXEC Master.dbo.xp_fileexist 'g:\abc.bak', @IsFileExists OUT  
  
  BEGIN  
      IF @IsFileExists = 1  
      BEGIN  
          PRINT 'File Exists in the G:\ location'  
      END  
      ELSE  
      BEGIN
          DECLARE @htmlbody VARCHAR(4000)
  
          EXEC msdb.dbo.sp_send_dbmail
                    @profile_name = 'EMAIL',
                    @recipients = 'ylx@xly.com',
                    @subject = 'Database Mail Test'
          PRINT 'File Does not Exist in the G:\ location'
      END --IF
  END
  </code></pre>
  
  <p>I'm not receiving any email alert: </p>
  
  <p>Here is the output:</p>
  
  <blockquote>
    <p>(1 row affected)<br>
    Mail (Id: 1065) queued.<br>
    File Does not Exist in the g:\ location</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/wOfDA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wOfDA.png" alt="enter image description here"></a></p>
  
  <p>Description: The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2019-12-11T19:00:19). Exception Message: Cannot send mails to mail server. (Failure sending mail.).  )</p>
  
  <p>All SQL services are running, executed the script as a sysadmin user. 
  Can someone please advise:</p>