Email sending error Could not load file or assembly 'System.Text.Encoding.CodePages'

Jason Herz asked on February 12, 2024 21:33

Hello,

I am currently having trouble getting emails to send after upgrading from 13.0.43 to 13.0.136.

I have the following binding redirect in my admin side web.config and ensured I have MimeKit 3.3.0 and System.Text.Encoding.CodePages 6.0.0 installed:

  <dependentAssembly>
    <assemblyIdentity name="System.Text.Encoding.CodePages" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
  </dependentAssembly>

I am using Office 365 (Exchange) SMTP settings and I've run tests by going to Email Queue>Send email and sending an email to myself.

The emails don't send and I am given the following error message:

'The type initializer for 'MimeKit.ParserOptions' threw an exception. The type initializer for 'MimeKit.Utils.CharsetUtils' threw an exception. Could not load file or assembly 'System.Text.Encoding.CodePages, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) Could not load file or assembly 'System.Text.Encoding.CodePages, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'

I can force the emails to send by clicking the Resend all failed button, and the mails send right away.

How can I get the emails to send on their own?

Thank you, Jason

Recent Answers


Juraj Ondrus answered on February 13, 2024 05:41

How was the hotfix applied? I would recommend restoring backups and maybe try again. There were some changes in the NuGet packages versions as well as new engine/library for sending emails was introduced. Check what version of System.Text.Encoding.CodePages package is installed in the admin app? Previously it was 4.x and now it should be 6.0. You can also try reinstalling it and rebuilding the admin app. Check also any custom modules which are referencing Kentico libraries.

0 votesVote for this answer Mark as a Correct answer

Jason Herz answered on February 14, 2024 01:11

We applied the hotfix using the Kentico Hotfix Utility, and we currently have System.Text.Encoding.CodePages version 6.0.0 installed. We will try to reapply the hotfix and I will update this ticket with our status when we're done.

Thanks for your suggestions

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on February 14, 2024 06:15

Are you able to build the admin app in Visual studio without errors?

0 votesVote for this answer Mark as a Correct answer

Jason Herz answered on February 14, 2024 20:59

Yes, we were able to build the admin app in VS without errors. We applied Hotfix 138 to the site and updated the MimeKit nuget package to resolve that error message (we saw several others having the same issue not related to Kentico https://github.com/jstedfast/MimeKit/issues/792)

We've deployed our changes to the live site and are no longer seeing the MimeKit error, however emails are still not sending. Now instead of the emails failing, they're stuck in the Sending status. We've verified the Kentico Xperience Scheduler service is running in Windows Services and that the Scheduled task "Send queued emails" is functioning.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on February 15, 2024 09:29

Are there any errors in the Event log? Are the emails sent when you try resending them manually? Could you please double check the SMTP setup - in Kentico as well as SMTP server side? Are you using valid and trusted SSL certificates? I would also try testing the sending with PaperCur ot SMTP4Dev servers as described here.

0 votesVote for this answer Mark as a Correct answer

Jason Herz answered on February 16, 2024 00:57

Are there any errors in the Event log? when the scheduled task 'Send queued emails' runs for the first time after a test email is submitted we get the following error:

Event ID:467132
Event type:Error
Event time:2/15/2024 3:44:06 PM
Source:EmailEngine
Event code:SMTPServerLookupTable
IP address:
Description:Message: An item with the same key has already been added.
Exception type: System.ArgumentException
Stack trace:
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at CMS.EmailEngine.SMTPServerLookupTable.AddDefaultServer(String siteName, Int32 siteId, List`1 serverIDs)
at CMS.EmailEngine.SMTPServerLookupTable.AddChain(String siteName)
at CMS.EmailEngine.SMTPServerLookupTable.Next(String siteName, SMTPServerTokenData& smtpServer)
at CMS.EmailEngine.SMTPServerLookupTable.AcquireNext(String siteName)

Are the emails sent when you try resending them manually? No

Could you please double check the SMTP setup - in Kentico as well as SMTP server side? we don't have anything in the SMTP Servers module of Kentico, only in Settings>System>Email, and we're using Office 365 Exchange to send emails... everything appears to be configured appropriately

Are you using valid and trusted SSL certificates? Yes

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on February 16, 2024 05:30

It seems that you have some duplicate SMTP Servers in the database. Let's verify this - can you please send me the result of the following SQL queries?

  SELECT KeyID, KeyName, KeyValue, SiteID FROM CMS_SettingsKey

  WHERE KeyName = 'CMSSMTPServer'



  SELECT ServerID, ServerName, ServerIsGlobal, ServerGUID FROM CMS_SMTPServer

Also, do you have any custom code working with the SMTP servers?

I can also imagine that the SiteID of the current site in the database is 0, which is an ID we use to add the Global SMTP server. Can you please check the following query:

SELECT SiteID, SiteName FROM CMS_Site

Is it returning any site where SiteID is 0?

Also, what are the scheduler settings in Settings -> System and then in the Scheduler section -> Application scheduler interval? Is it greater than 0?

0 votesVote for this answer Mark as a Correct answer

Jason Herz answered on February 16, 2024 20:23

Thanks for your help!!!

The first query returned the following

Image Text

The second query returned nothing

Image Text

Also, do you have any custom code working with the SMTP servers?

No

Is it returning any site where SiteID is 0?

SELECT SiteID, SiteName FROM CMS_Site

No this query returned the only site as SiteID = 1

Also, what are the scheduler settings in Settings -> System and then in the Scheduler section -> Application scheduler interval? Is it greater than 0?

The Application scheduler interval is set to 10 seconds

Image Text

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on February 19, 2024 07:52

This is strange. In the past when we saw this issue there was an orphaned record for a SMTP server definition. HAve you tried using the PaperCut or SMTP4Dev just for testing? Just to change the settings to something else, save. Perhaps test the email sending to see if it makes any difference and then return the previous settings back?
Also, the SMTP Server settings are set on the global level right? Are there any site level settings set in Settings app? When you use the upper left hand side drop down to switch between global and site level settings.

0 votesVote for this answer Mark as a Correct answer

Jason Herz answered on February 21, 2024 18:02

We rebooted the server the other day and now we are no longer seeing the error message in Kentico > Event Log. However, now we are seeing the following error in the Windows Event Viewer:

Application: SchedulerService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileLoadException
   at CMS.EmailEngine.EmailProvider+<SendWithMailKitAsync>d__16.MoveNext()
      at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[CMS.EmailEngine.EmailProvider+<SendWithMailKitAsync>d__16, CMS.EmailEngine, Version=13.0.13.0, Culture=neutral, PublicKeyToken=834b12a258f213f9]](<SendWithMailKitAsync>d__16 ByRef)
         at CMS.EmailEngine.EmailProvider.SendWithMailKitAsync(System.Net.Mail.MailMessage, CMS.EmailEngine.EmailToken)
            at CMS.EmailEngine.EmailProvider+<SendEmailAsyncInternal>d__14.MoveNext()
               at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                  at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
                     at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
                        at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
                           at System.Threading.ThreadPoolWorkQueue.Dispatch()
0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on February 22, 2024 05:48

What are the scheduler settings? Are you using the Windows scheduler service and Kentico system is set to use external scheduler as well as the Send queued emails scheduled task? Is the scheduler service using the same hotfix as the project? E.g. when applying hotfix, did you applied it on the setup files and external utils as well? Is the service running, does it have appropriate DB permissions?

0 votesVote for this answer Mark as a Correct answer

Jason Herz answered on February 23, 2024 18:54

We are using the Windows service: Kentico Xperience Scheduler, and Kentico is using the Send Queued emails task, set to run every minute. We checked the SchedulerService.exe file and the version is on the same hotfix version as the site (138) Image Text

Yes, the service is running and no database permissions have changed. We reset file permissions on the web server to make sure nothing was incorrect.

We are also seeing this error in the Windows Event Viewer:

Faulting application name: SchedulerService.exe, version: 13.0.138.39448, time stamp: 0x65c4dba7
Faulting module name: KERNELBASE.dll, version: 10.0.14393.6343, time stamp: 0x65028150
Exception code: 0xe0434352
Fault offset: 0x0000000000026ea8
Faulting process id: 0xfb8
Faulting application start time: 0x01da668077cae715
Faulting application path: E:\Websites\MSI-K13\Admin\bin\SchedulerService.exe
Faulting module path: C:\Windows\System32\KERNELBASE.dll
Report Id: 5dcd0259-8c08-406c-9641-d151499bd29a
Faulting package full name: 
Faulting package-relative application ID: 

Thanks again for all your help!

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on February 26, 2024 05:39

Thanks. The version seems to be OK. I would also try these things:

  • installing a fresh Kentico instance test it there and then compare the web.config files to see if there is any difference, use the default config file in customer’s project (just adjust the connection string and hash salt string key).

  • I would also check what is the value of the CMSApplicationName and CMSApplicationGuid keys in the main web.onfig file. In the

  • make sure that in the admin project, this file is present: \CMS\App_Data\CMSModules\WinServices\services.xml

  • is there any KenticoCMSScheduler(< application name >).srvc file in the same location? (its content is just date/time stamp of the service installation.)

  • Make sure that the disk permissions to this location are granted.

  • Also, is it possible that you have updated some NuGet packages in the admin solution? If yes, then this could be the reason of the assembly error mentioned above. Adding the assembly bindings also to the \CMS\bin\SchedulerService.exe.config file could help (but restarting the service may revert the changes back to defaults, but at least it could be a good pointer where to look further).

And I would also double check the DB permissions. When we saw similar issues in the past, it was caused by wrong DB access. I did a little digging and I think I found the steps that might help this permissions issue a bit easier. I was able to find the steps to set the user permissions on the database directly from the Microsoft site:

"To add a SQL login or msdb role to a SQL Server Agent fixed database role

As for running the Scheduler Windows service as the Local System account, I found this tutorial:

https://superuser.com/questions/347751/how-can-i-run-a-scheduled-task-as-system

0 votesVote for this answer Mark as a Correct answer

Michael Rahel answered on March 20, 2024 06:18

Was this ever resolved? We seem to be experiencing the exact same issue. We are required to use the External Windows Scheduler service and it crashes and mail won't send and we're seeing the same errors. I have tried most of what was proposed here to test this and have yet to come to a solution. Any additional help would be much appreciated!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.