Home>

Ready to migrate some projects to asp.net coreWhen encountering the mail sending class, I found it in asp.net core 1.0 and offered to provide the smtp related class library.So a search online found mailkit

Good things must be tried,Not to mention open source,The following is the code to achieve smtp mail sending:

using mailkit.net.smtp;
using mailkit.security;
using mimekit;
using system.threading.tasks;
namespace consoleapp1
{
 public class mailhelper
 {
  public static void send (string email, string subject, string message)
  {
   var emailmessage=new mimemessage ();
   emailmessage.from.add (new mailboxaddress ("tianwei blogs", "[email protected]"));
   emailmessage.to.add (new mailboxaddress ("mail", email));
   emailmessage.subject=subject;
   emailmessage.body=new textpart ("plain") {text=message};
   using (var client=new smtpclient ())
   {
    client.connect ("smtp.hantianwei.cn", 465, true);
    client.authenticate ("[email protected]", "******");
    client.send (emailmessage);
    client.disconnect (true);
   }
  }
  public static async task sendemailasync (string email, string subject, string message)
  {
   var emailmessage=new mimemessage ();
   emailmessage.from.add (new mailboxaddress ("tianwei blogs", "[email protected]"));
   emailmessage.to.add (new mailboxaddress ("mail", email));
   emailmessage.subject=subject;
   emailmessage.body=new textpart ("plain") {text=message};
   using (var client=new smtpclient ())
   {
    await client.connectasync ("smtp.hantianwei.cn", 25, securesocketoptions.none) .configureawait (false);
    await client.authenticateasync ("[email protected]", "******");
    await client.sendasync (emailmessage) .configureawait (false);
    await client.disconnectasync (true) .configureawait (false);
   }
  }
 }
}

The above code is synchronous and asynchronous.

  • Previous Some comprehensive understanding of Js OOP programming to create objects
  • Next Android uses Vitamio to create its own universal player (10)-local playback (thumbnails, video information, video scanning servi