asp.net Core 1.0 implementation of the mail delivery function _ practical skills

Source: Internet
Author: User
Tags smtpclient aliyun

Preparing to migrate some projects to ASP.net core start with the Encapsulation Class library and find the SMTP-related class library in ASP.net Core 1.0 when you encounter the mail delivery class, so a search on the internet found Mailkit

Good things must try, not to mention open source, the following is the code to implement SMTP mail delivery:

Using MAILKIT.NET.SMTP;
Using Mailkit.security;
Using Mimekit;

Using System.Threading.Tasks; Namespace ConsoleApp1 {public class Mailhelper {public static void Send (string e-mail, string subject, String message
   ) {var emailmessage = new MimeMessage ();
   EMAILMESSAGE.FROM.ADD (New mailboxaddress ("Tianwei blogs", "mail@hantianwei.cn"));
   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 ("mail@hantianwei.cn", "Hu Jintao"); Client.
    Send (Emailmessage); Client.

   Disconnect (TRUE); } public static Async Task Sendemailasync (string e-mail, string subject, String message) {var emailmessage = n

   EW MimeMessage ();
   EMAILMESSAGE.FROM.ADD (New mailboxaddress ("Tianwei blogs", "mail@hantianwei.cn")); 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", Securesocketoptions.none).
    Configureawait (FALSE); Await client.
    Authenticateasync ("mail@hantianwei.cn", "Hu Jintao"); Await client. SendAsync (Emailmessage).
    Configureawait (FALSE); Await client. Disconnectasync (True).
    
   Configureawait (FALSE); 

 }
  }

 }
}

The code above is synchronous and asynchronous.
Note: General mailbox such as Tencent Enterprise Mail, 163 can send success, but Aliyun mail push failed, if there is a master can realize Aliyun push mail please tell me, thank you very much!

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.