eric1825
eric1825

Reputation: 415

The type initializer for 'MimeKit.ParserOptions' threw an exception

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("[email protected]")
    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("[email protected]", "****")
        End If
        client.Send(mimeMail)
        client.Disconnect(True)
    End Using

Upvotes: 1

Views: 2608

Answers (2)

MrCalvin
MrCalvin

Reputation: 1825

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

Upvotes: 2

jstedfast
jstedfast

Reputation: 38593

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?

Upvotes: 0

Related Questions