0

I am testing using an existing System.Net.Mail.MailMessage with MimeKit's support for direct casting to a MimeMessage in addition to using MimeKit's DkimSigner and MailKit's Smtp client.

I am getting "The type initializer for 'MimeKit.ParserOptions' threw an exception." With a stack trace mentioning 'at MimeKit.MimeMessage.CreateFromMailMessage(MailMessage message)'

There is also an Inner Excpetion: "The type initializer for 'MimeKit.Utils.CharsetUtils' threw an exception." Stacktrace: 'at MimeKit.ParserOptions..ctor() at MimeKit.ParserOptions..cctor()

I am not getting any exception on my development box but that only executes up to the conversion and signing not the actual smtp sending.

    Dim netMail As New System.Net.Mail.MailMessage
    netMail.From = New System.Net.Mail.MailAddress("no_reply@lionandlambchurch.com")
    netMail.To.Add(txtTo.Text)
    netMail.Subject = txtSubject.Text
    netMail.Body = txtContent.Text

    Dim mimeMail As MimeMessage = CType(netMail, MimeMessage)

    Dim headersToSign = New List(Of HeaderId)
    headersToSign.Add(HeaderId.From)
    headersToSign.Add(HeaderId.To)
    headersToSign.Add(HeaderId.Subject)
    headersToSign.Add(HeaderId.Date)

    Dim privateKeyPath = AppDomain.CurrentDomain.BaseDirectory + "\App_Data\rsa.private"
    Dim signer = New Cryptography.DkimSigner(privateKeyPath, "lionandlambchurch.com", "key1")

    Dim loggerPath = AppDomain.CurrentDomain.BaseDirectory + "\logs\smtp-mailkit.log"

    mimeMail.Sign(signer, headersToSign, Cryptography.DkimCanonicalizationAlgorithm.Relaxed, Cryptography.DkimCanonicalizationAlgorithm.Simple)

    ' Don't attempt sending locally
    If Request.Url.Host.ToLower().Contains("localhost") Then Return

    Using client As New MailKit.Net.Smtp.SmtpClient(New ProtocolLogger(loggerPath))
        client.Connect("relay-hosting.secureserver.net", 25, False)
        If chkAuthenticate.Checked Then
            client.Authenticate("no_reply@lionandlambchurch.com", "****")
        End If
        client.Send(mimeMail)
        client.Disconnect(True)
    End Using
jstedfast
  • 35,744
  • 5
  • 97
  • 110
eric1825
  • 405
  • 3
  • 13

2 Answers2

2

Had same problem and I solved it by opening the NuGet console in Visual Studio and installing the newest System.Text.Encoding.CodePages package:

install-Package System.Text.Encoding.CodePages

MrCalvin
  • 1,675
  • 1
  • 19
  • 27
0

Based on the exception, the error is occurring during the conversion, not sending.

For some reason, the static constructor for MimeKit.ParserOptions is failing because the static constructor for MimeKit.Utils.CharsetUtils is failing.

Looking at MimeKit's code, all I can think of is that your server doesn't have UTF-8 or Latin1 support.

e.g. System.Text.Encoding.GetEncoding (65001, new EncoderExceptionFallback (), new DecoderExceptionFallback ()); or Encoding.GetEncoding (28591, new EncoderExceptionFallback (), new DecoderExceptionFallback ()); is throwing an exception.

Honestly, I don't know how that could even happen.

What .NET are you using on your server?

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • This is on a godaddy server. Using .NET 4.5 – eric1825 Feb 09 '16 at 15:18
  • Can you confirm that it doesn't have UTF8 and/or Latin1 support? – jstedfast Feb 09 '16 at 16:22
  • I confirmed, their shared windows hosting only supports UTF-8 and not Latin1 – eric1825 Feb 10 '16 at 01:23
  • Is it possible for you to get Latin1 support? – jstedfast Feb 10 '16 at 01:45
  • The respoonse I got was their Linux plans will support Latin1, but not the Windows plans, "There is not a way to add it". – eric1825 Feb 10 '16 at 13:43
  • Okay, so one possible work-around is to use the PCL versions of MimeKit and MailKit because they use my Portable.Text.Encoding library for charset conversion rather than the System encodings. – jstedfast Feb 10 '16 at 14:11
  • I have no previous experience yet with PCL's. Could you rough out the steps needed for that? I did clone your MimeKit repo locally and opened the MimeKit.sln but it would not open three of the projects including Android, CoreFX, and iOS (I need Xamirin?). The Portable project would not compile. I am using Visual Studio 2013 Community Edition Update 4. I just installed Microsoft.Net Portable Library Reference Assemblies after the first attempt. Still not building. Or is there already a compiled PCL version available? What other considerations are there for my web app's build? – eric1825 Feb 13 '16 at 22:22
  • Xamarin is needed for the iOS and Android projects, yes, but you can just disable (or remove) those projects. The CoreFX project requires VS2015 (you can remove it as well). What build error(s) are you getting? – jstedfast Feb 14 '16 at 00:29
  • I installed VS Comunity 2015 and things are building minus the ios/android projects. I am getting the MimeKit.dll and Portable.Text.Encoding.dll from the release build of the portable project. Can I use this with the MailKit.dll I already got from Nuget? Not seeing portable being mentioned in the MailKit repo page on github, so I don't need to compile that repo? – eric1825 Feb 14 '16 at 00:37
  • Ewww.... I'm not sure, to be honest. The PCL version of MimeKit was intended to be used with the WindowsPhone version of MailKit (I forgot that I didn't have a true PCL version of MailKit), but it *might* work with the .NET 4.5 version of MailKit as well, I just haven't tested that pairing. The only potential gotcha that I can think of is that MimeKit uses Portable.Text.Encoding while MailKit uses System.Text.Encoding in that scenario, but that might not matter. – jstedfast Feb 14 '16 at 13:30
  • BTW, make sure to reference Portable.Text.Encoding.dll because the PCL MimeKit.dll needs it. – jstedfast Feb 14 '16 at 13:31
  • Since you are using nugets, you *might* be able to edit your `packages.config` and add these: ` ` – jstedfast Feb 14 '16 at 13:53
  • I'm starting to doubt this will work... I forgot that MailKit also makes use of BouncyCastle and it may not like the PCL BouncyCastle :-\ – jstedfast Feb 14 '16 at 13:54
  • If this PCL trick doesn't work, can you find out if `System.Text.Encoding.GetEncoding (1252);` works and whether or not `System.Text.Encoding.GetEncoding (20127);` works? Maybe I can use one of these as a fallback if `28591` fails. – jstedfast Feb 14 '16 at 14:07
  • I've patched MimeKit on GitHub to recover from a lack of `iso-8859-1` support on the system by trying to first fall back to `windows-1252` and then to `us-ascii`. I'm pretty sure that at least `us-ascii` is part of the core. Would be nice if you could test which of the 2 encodings I listed above are present on your system, though, so that I can be sure that my assumptions are correct. – jstedfast Feb 15 '16 at 22:31
  • I will test those encodings and report back – eric1825 Feb 15 '16 at 22:38
  • System.Text.Encoding.GetEncoding(1252) works and System.Text.Encoding.GetEncoding(20127) works – eric1825 Feb 16 '16 at 06:59
  • Awesome, thanks! This suggests that my work-around should fix things for you. You can probably just build the .NET 4.5 version of MimeKit and drop the MimeKit.dll into your project instead of using the MimeKit nuget (the only change is this work-around). – jstedfast Feb 16 '16 at 14:25
  • I had to patch MailKit in GitHub as well, since it also expects iso-8859-1 to be there, so you'll need to grab that as well. The next release will have these fixes. – jstedfast Feb 16 '16 at 22:22
  • Compiled your code with the patch. Sorry still seeing the same exceptions in my error logger. I did however turn off custom exceptions to see the ASP.NET red screen in full. My error logger evidently only logged two exception levels. Actually there are three exceptions reported. The third one and first in the red screen stack trace is – eric1825 Feb 18 '16 at 17:05
  • [VerificationException: Operation could destabilize the runtime.] MimeKit.Utils.OptimizedOrdinalIgnoreCaseComparer.GetHashCode(String obj) +17 System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +66 MimeKit.Utils.CharsetUtils.AddAliases(Dictionary`2 dict, Int32 codepage, Int32 fallback, String[] names) +81 MimeKit.Utils.CharsetUtils..cctor() +516 – eric1825 Feb 18 '16 at 17:05
  • This may be useful: http://dotnet4hosting.asphostcentral.com/post/SOLVED-Operation-could-destabilize-the-runtime-ASPNET-45-Hosting.aspx – jstedfast Feb 18 '16 at 17:10
  • Or this: http://stackoverflow.com/questions/23683182/operation-could-destabilize-the-runtime-in-structuremap – jstedfast Feb 18 '16 at 17:10