““MailKit.Net.Smtp.SmtpClient”的类型初始值设定项;抛出异常”
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您使用的是 WASM(又名 Blazor WebAssembly< /a>),您可能无法使用 SMTP 客户端。
引用 GitHub 上类似问题的答案:
这也是我实际要做的:
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:
This is what I would actually do, too: