““MailKit.Net.Smtp.SmtpClient”的类型初始值设定项;抛出异常”

发布于 2025-01-12 15:15:15 字数 2787 浏览 3 评论 0原文

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

我在尝试实例化 SmtpClient 时收到此错误“'MailKit.Net.Smtp.SmtpClient'的类型初始值设定项引发异常”此源代码

//新信息: InnerExeption 是:'System.PlatformNotSupportedException:此平台不支持 System.Net.NetworkInformation。在 System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties() 在 MailKit.Net.Smtp.SmtpClient..cctor()' – 那么现在,MS 推荐 MailKit,然后 MailKit 不支持 .Net 6。这是什么原因?

来自 MSDN“SmtpClient 类”

备注

SmtpClient 类用于将电子邮件发送到 SMTP 服务器 用于交货。 SMTP 协议在 RFC 2821 中定义,它是 可在 https://www.ietf.org 获取。

重要

我们不建议您将 SmtpClient 类用于新的 开发,因为 SmtpClient 不支持许多现代协议。 改用 MailKit 或其他库。有关详细信息,请参阅 SmtpClient 不应在 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("[email protected]"));
    email.To.Add(MailboxAddress.Parse("[email protected]"));
    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("[email protected]", "ppppppwwwwww");
        smtp.Send(email);
        smtp.Disconnect(true);
    } catch (Exception ex) {
        Console.WriteLine($"\r\n\n\nError sending email is '{ex.Message}'.");
    }
  }
}

In VS-2022, .Net 6, Blazor WASM, C#, MailKit 3.1.1.

I am getting this error "The type initializer for 'MailKit.Net.Smtp.SmtpClient' threw an exception" on this source-code when trying to instantiate SmtpClient:

// NEW INFORMATION:
InnerExeption is: 'System.PlatformNotSupportedException: System.Net.NetworkInformation is not supported on this platform. at System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties() at MailKit.Net.Smtp.SmtpClient..cctor()' –
So now, MS recommends MailKit and then MailKit does not support .Net 6. What gives?

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.

Did .Net 6 interfere with MailKit working? Your comments are welcome...Thanks

@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("[email protected]"));
    email.To.Add(MailboxAddress.Parse("[email protected]"));
    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("[email protected]", "ppppppwwwwww");
        smtp.Send(email);
        smtp.Disconnect(true);
    } catch (Exception ex) {
        Console.WriteLine(
quot;\r\n\n\nError sending email is '{ex.Message}'.");
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末蓝 2025-01-19 15:15:15

由于您使用的是 WASM(又名 Blazor WebAssembly< /a>),您可能无法使用 SMTP 客户端。

引用 GitHub 上类似问题的答案:

SMTP 客户端不太可能在 Blazor WebAssembly 中工作。最终,Blazor 受到与浏览器中运行的任何其他应用程序相同的限制。

这意味着您无法创建 TCP 或 UDP 连接,因为浏览器不允许这样做。

您可能需要通过 Rest API 从服务器上的端点执行此操作。

这也是我实际要做的:

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

Since you are using WASM (aka Blazor WebAssembly), you will probably be not able to use an SMTP client client-side.

To quote an answer to a 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.

This is what I would actually do, too:

  1. Create a server-side ASP.NET Core Web API endpoint.
  2. Protect the API endpoint with some API key to not expose it publicly.
  3. Pass the related data from your WASM to that Web API.
  4. Send the actual email message via MailKit from that server-side API.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文