除了使用 MimeKit 的 DkimSigner 和 MailKit 的 Smtp 客户端之外,我正在使用现有的 System.Net.Mail.MailMessage 进行测试,MimeKit 支持直接转换为 MimeMessage。
我收到“'MimeKit.ParserOptions' 的类型初始化程序引发了异常。” 堆栈跟踪提到“在 MimeKit.MimeMessage.CreateFromMailMessage(MailMessage 消息)”
还有一个内部异常:“'MimeKit.Utils.CharsetUtils' 的类型初始化程序引发了异常。” Stacktrace:'在 MimeKit.ParserOptions..ctor() 在 MimeKit.ParserOptions..cctor()
我的开发框没有任何异常,但只执行到转换和签名,而不是实际的 smtp 发送。
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