"The type initializer for 'MailKit.Net.Smtp.SmtpClient' threw an exception"

"The type initializer for 'MailKit.Net.Smtp.SmtpClient' threw an exception"

在 VS-2022、.Net 6、Blazor WASM、C#、MailKit 3.1.1 中。

我在尝试实例化 SmtpClient 时在此源代码 上收到此错误 "The type initializer for 'MailKit.Net.Smtp.SmtpClient' threw an exception":

// 新信息: InnerExeption 是:'System.PlatformNotSupportedException: System.Net.NetworkInformation is not supported on this platform. at System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties() at MailKit.Net.Smtp.SmtpClient..cctor()' – 所以现在,MS 推荐 MailKit,然后 MailKit 不支持 .Net 6。什么原因?

from MSDN "SmtpClient Class"

Remarks

The SmtpClient class is used to send email to an SMTP server for delivery. The SMTP protocol is defined in RFC 2821, which is available at https://www.ietf.org.

Important

We don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols. Use MailKit or other libraries instead. For more information, see SmtpClient shouldn't be used on GitHub.

.Net 6 是否干扰了 MailKit 的工作?欢迎您提出意见...谢谢

@page "/mailkittestpage"

@using Microsoft.AspNetCore.Components.Forms
@using myVSProject.Client.Services
@using myVSProject.Shared
@using System.ComponentModel.DataAnnotations
@inject NavigationManager _navigationManager
@using System.Net.Http
@using System.Net.Http.Json
@inject HttpClient Http
@using MailKit.Net.Smtp
@using MailKit.Security
@using MimeKit
@using MimeKit.Text

<h3>Email Test Page</h3>
<hr />
<div class="button-container">
   <button type="button" class="e-btn e-normal e-primary" @onclick="SendEmail">Send Email</button>
</div>

@code {
    //>>> other code omitted <<<//

    public void SendEmail() {
        
    // Button-click event to Send Email.
    email.From.Add(MailboxAddress.Parse("aaaaaaaaa@hotmail.com"));
    email.To.Add(MailboxAddress.Parse("bbbbbbbbbb@outlook.com"));
    email.Subject = "Email Subject goes here";
    email.Body = new TextPart(TextFormat.Plain) { Text = "This is the body of the email" };

    // send email
    try {
        using var smtp = new MailKit.Net.Smtp.SmtpClient();          <== this line FAILS.

        smtp.ServerCertificateValidationCallback = (s, c, h, e) => true;
        smtp.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);          
        smtp.Authenticate("aaaaaaaaa@outlook.com", "ppppppwwwwww");
        smtp.Send(email);
        smtp.Disconnect(true);
    } catch (Exception ex) {
        Console.WriteLine($"\r\n\n\nError sending email is '{ex.Message}'.");
    }
  }
}

由于您使用的是 WASM(又名 Blazor WebAssembly),您可能无法使用 SMTP 客户端 client-side。

引用一个similar issue on GitHub的答案:

It is unlikely than an SMTP client is going to work in Blazor WebAssembly. Ultimately Blazor is subject to the same restrictions that any other application running in the browser.

That means you can't create TCP or UDP connections since browsers don't allow that.

You'll likely need to do this from an endpoint on a server through a Rest API.

这也是我实际会做的:

  1. 创建一个 server-side ASP.NET Core Web API 端点。
  2. 使用一些 API 密钥保护 API 端点,使其不公开。
  3. 将相关数据从您的 WASM 传递到该 Web API。
  4. 通过 MailKit 从 server-side API 发送实际的电子邮件。