Search code examples
c#sql-serversqlconnectionews-managed-api

EWS Managed API and SQL Server - Login failed. The login is from an untrusted domain and cannot be used with Windows authentication


I have a process that uses EWS Managed API to save email messages as eml files. Like this:

var propertySet = new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent, EmailMessageSchema.IsRead);
message.Load(propertySet);
File.WriteAllBytes(fullPath, message.MimeContent.Content);

After the file is saved, I add a record to the database. The problem is, when the application is done saving the eml for particularly large emails, I cannot connect to SQL Server. I get "Login failed. The login is from an untrusted domain and cannot be used with Windows authentication" when this code gets to here:

using (SqlConnection conn = new SqlConnection(connString))
{
     conn.Open();

None of the other posts with this error message seem to fit my situation. I can connect to SQL Server with no problems until I run across a large email message.


Solution

  • Why not just keep your connection to SQL open while your process is running? Do you need to re-establish it for every message? If so - you will quickly run out of available ports or reach a user limit depending on how SQL Server is configured and your licensing model. You should do a netstat and check how the connections grow. Eventually these connections will be freed, but it is something to consider.

    Are you using single or multi-threaded application model when interfacing and processing the EML files?