Databasemail.exe not working on Windows 2012, Running SQL2017

DebayanBhattacharya-5228 1 Reputation point
2020-09-25T07:21:29.63+00:00

Hi, I have an SQL2017 server installed in my machine and I have admin access to it. I've configured databasemail in my instance.

Ensured:

> Remote login is enabled in the database
> .NET 3.5 is installed on the server
> We are able to send emails using the same SMTP server through powershell

Observation:

> "exec msdb..sysmail_help_status_sp" gives an output of "STARTED"
> test emails are always getting queued and never processed
> even if we delete unsent items, "exec msdb..sysmail_help_queue_sp" gives 44 as length for mail column and this keeps increasing the more test mails we send. State is always "INACTIVE"
> Double clicking databasemail.exe starts the process and stops immediately. Starting it from command prompt gives the error:The type initializer for 'Microsoft.SqlServer.Management.SqlIMail.Server.Common.Logger' threw an exception.
> Running databasemail.exe as an administrator logs an event in the applications event log as follow (and notice how in the event viewer log, the connection parameter has an empty server name. how do I fix it?)

Log Name: Application
Source: DatabaseMail
Date: 9/24/2020 2:32:44 PM
Event ID: 0
Task Category: None
Level: Error
Keywords: Classic
User: N/A
Computer: <<COMPUTERNAME>>
Description:

1) Exception Information

Exception Type: Microsoft.SqlServer.Management.SqlIMail.Server.Common.BaseException
Message: There was an error on the connection. Reason: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server), connection parameters: Server Name: ., Database Name: msdb
Data: System.Collections.ListDictionaryInternal
TargetSite: Void OpenConnection(Microsoft.SqlServer.Management.Common.SqlConnectionInfo)

HelpLink: NULL
Source: DatabaseMailEngine
HResult: -2146232832

StackTrace Information

at Microsoft.SqlServer.Management.SqlIMail.Server.DataAccess.ConnectionManager.OpenConnection(SqlConnectionInfo connectionInfo)
at Microsoft.SqlServer.Management.SqlIMail.IMailProcess.QueueItemProcesser.ProcessQueueItems(String dbName, String dbServerName, Int32 lifetimeMinimumSec, LogLevel loggingLevel, Byte[] encryptionKey, Int32 connectionTimeout)

2) Exception Information

Exception Type: System.Data.SqlClient.SqlException
Errors: System.Data.SqlClient.SqlErrorCollection
ClientConnectionId: 00000000-0000-0000-0000-000000000000
Class: 20
LineNumber: 0
Number: 2
Procedure: NULL
Server:
State: 0
Source: .Net SqlClient Data Provider
ErrorCode: -2146232060
Message: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Data: System.Collections.ListDictionaryInternal
TargetSite: Boolean TryGetConnection(System.Data.Common.DbConnection, UInt32, Boolean, Boolean, System.Data.Common.DbConnectionOptions, System.Data.ProviderBase.DbConnectionInternal ByRef)
HelpLink: NULL
HResult: -2146232060

StackTrace Information

at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at Microsoft.SqlServer.Management.SqlIMail.Server.DataAccess.ConnectionManager.OpenConnection(SqlConnectionInfo connectionInfo)

3) Exception Information

Exception Type: System.ComponentModel.Win32Exception
NativeErrorCode: 2
ErrorCode: -2147467259
Message: The system cannot find the file specified
Data: System.Collections.ListDictionaryInternal
TargetSite: NULL
HelpLink: NULL
Source: NULL
HResult: -2147467259
I've tried everything that I can think of:

> ensured I have access privileges to databasemail.exe
> restarted SQL services several times
> recreated profiles and accounts (with the same SMTP server)
> Enabled NamedPipes
> Remote login to the database instance is possible

Any ideas or solutions are highly appreciated!!! This is causing a pain in our environment and we need to enable databasemail feature.

History

> The database was earlier hosted under SQL 2008, and it is migrated to SQL 2017 now. the DBs work fine.
> Ports we are using "AND CANNOT BE CHANGED" are different from default ports

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,998 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,656 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Tom Phillips 17,741 Reputation points
    2020-09-25T19:05:07.3+00:00

  2. AmeliaGu-MSFT 13,986 Reputation points Microsoft Vendor
    2020-09-28T05:45:51.107+00:00

    Hi @DebayanBhattacharya-5228 ,

    Could you please check if SQL Server Browser is running?
    In addition, you can try to create alias for SQL Server in SQL Server Configuration Manager.
    Please refer to the following similar threads which might help:
    Database Mail Error
    Unable to send mail from SQL server using DatabaseMail

    Best Regards,
    Amelia


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. AmeliaGu-MSFT 13,986 Reputation points Microsoft Vendor
    2020-09-29T08:21:24.27+00:00

    Hi @DebayanBhattacharya-5228 ,
    Thanks for your reply.

    --Databasemail.exe runs briefly when double clicked on it and exits automatically.

    When there are e-mail messages to be processed, Database Mail uses Service Broker activation to start the external program (DatabaseMail.exe). If there are no e-mail messages to be processed, the DatabaseMail.exe will be shutting down automatically.
    29083-1.jpg

    --When trying to execute databasemail.exe from command prompt, it gives an "The type initializer for 'Microsoft.SqlServer.Management.SqlIMail.Server.Common.Logger' threw an exception" error.
    It seems this error is related to .NET Framework. Please refer to this thread which might help.

    --databasemail.exe.config
    Please refer to:
    SQL DATABASE MAIL NOT WORKING
    databasemail.exe.config(a text file)
    In addition, could you please try to use .NET Framework 4.6?

    Best Regards,
    Amelia


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.