ASP.NET Using mialkit to send mail in core

Time:2021-3-26

The specific codes are as follows:

#Guide bag
  First of all, we need to import the mailkit nuget package. The nuget installation package command is described below.
#Reference namespace
using MailKit.Net.Smtp;
using MimeKit;
#Mail sending help class
 /// <summary>
 ///Send mail
 /// </summary>
 ///< param name = "name" > sender's name < / param >
 ///< param name = "receive" > receiving mailbox < / param >
 ///< param name = "sender" > email address < / param >
 ///< param name = "password" > email password < / param >
 ///< param name = "host" > mailbox host < / param >
 ///< param name = "port" > mailbox port < / param >
 ///< param name = "subject" > email subject < / param >
 ///< param name = "body" > email content < / param >
 /// <returns></returns>
 public async Task<bool> SendMailAsync(string Name, string receive, string sender, string password, string host, int port, string subject, string body)
 {
  try
  {
          #Mimemessage represents the object of an email
  var message = new MimeMessage();
          #Add sender address name sender name sender mailbox
  message.From.Add(new MailboxAddress(Name, sender));
          #Add recipient address
  message.To.Add(new MailboxAddress("", receive));
          #Set message subject information
  message.Subject = subject;
          #Set email content
  var bodyBuilder = new BodyBuilder() { HtmlBody = body };
  message.Body = bodyBuilder.ToMessageBody();
  using (var client = new SmtpClient())
  {
   // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
   client.ServerCertificateValidationCallback = (s, c, h, e) => true;
   // Note: since we don't have an OAuth2 token, disable 
   // the XOAUTH2 authentication mechanism. 
   client.AuthenticationMechanisms.Remove("XOAUTH2");
   client.CheckCertificateRevocation = false;
   //client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
   client.Connect(host, port, MailKit.Security.SecureSocketOptions.Auto);
   // Note: only needed if the SMTP server requires authentication
   client.Authenticate(sender, password);
   await client.SendAsync(message);
   client.Disconnect(true);
   return true;
  }
  }
  catch (Exception ex)
  {
  }
  return false;
 }

With the help of this simple mail sending class, we can already realize the mail sending function.

#Expand (nuget common command)

1. Install the specified version: install package < package name > – version < version No. >

2. Update package: update package < package name >

3. Re install all nuget packages (the entire solution will be re installed)

  update-package -reinstall

4. Re install all nuget packages for the specified project

Update package – Project < project name > – reinstall

5. Normal uninstall: install package < package name > 0

6. Force uninstall: install package < package name > – force

summary

The above is the introduction of Xiaobian ASP.NET In the core, mialkit is used to realize the email sending function. I hope it can help you. If you have any questions, please leave me a message and Xiaobian will reply you in time. Thank you very much for your support to developer!
If you think this article is helpful to you, please reprint, please indicate the source, thank you!