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

Time:03-09

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 >
   <button type="button"  @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}'.");
    }
  }
}

CodePudding user response:

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.
  • Related