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.