Implementation of mail sending in ASP. NET Core (mandatory), asp. netcore
Preface
We know that currently. NET Core does not support the SMTP protocol. When I use the mail sending function, some third-party components are required for the purpose, today we will introduce two open-source email sending components: MailKit and FluentEmail. I will introduce them separately.
MailKit
In ASP. NET Core, MailKit can be used to send mails. It supports cross-platform and IMAP, POP3, SMTP, and other protocols.
You can use the following method to install:
Install-Package MailKit
The following is an example of sending an email:
Var message = new MimeMessage (); message. from. add (new MailboxAddress ("Joey Tribbiani", "joey@friends.com"); message. to. add (new MailboxAddress ("Mrs. chanandler Bong "," chandler@friends.com "); message. subject = "where do I go on Sunday? "; Message. body = new TextPart ("plain") {Text = "How do I go to the Palace Museum?"}; 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; client. connect ("smtp.friends.com", 587, false); // Note: since we don't have an oau2token, disable // the xoau2authentication mechanic. client. authenticationmechanic isms. remove ("xoau2"); // Note: only needed if the SMTP server requires authentication client. authenticate ("joey", "password"); client. send (message); client. disconnect (true );}
If the Body you want to send is HTML, you can use the following:
var bodyBuilder = new BodyBuilder();bodyBuilder.HtmlBody = @"<b>This is bold and this is <i>italic</i></b>";message.Body = bodyBuilder.ToMessageBody();
Fluent Email
Fluent Email is also an open-source project. You can use the Razor template to send emails and integrate third-party Email sending programs such as Mailgun. However, this package is only available in.. NET 4.6. You can use the following command to install it:
Install-Package FluentEmail.Razor
You can use the most basic method to send an email, which is as follows:
// Note :. NET 4.6 only supports Email. defaultSender = new SmtpSender (); var email = Email. from ("foo@email.com "). ("bar@email.com", "bob "). subject ("where do I go on Sunday? "). Body (" How do I go to the Palace Museum? "); Await email. SendAsync ();
Alternatively, you can use the Razor template to send:
// Note :. NET 4.6 only supports Email. defaultSender = new SmtpSender (); // Using Razor templating packageEmail. defaultRenderer = new RazorRenderer (); var template = "Dear @ Model. name, You are totally @ Model. compliment. "; var email = Email. from ("bob@hotmail.com "). to ("somedude@gmail.com "). subject ("woo nuget "). usingTemplate (template, new {Name = "Luke", Compliment = "Awesome "});
Email. defaultRenderer tells FulentEmail which Renderer to use (you can also implement your own), and then provides a template whose content is the template string of the Razor syntax, then use UsingTemplate for rendering.
Cshtml template on disk
The file added to your email Razor template is relatively large and is not very elegant in character strings. You can place the template file on the disk and load it as follows:
// Note :. NET 4.6 only supports Email. defaultSender = new SmtpSender (); Email. defaultRenderer = new RazorRenderer (); var email = Email. from ("foo@email.com "). ("bar@email.com", "bob "). subject ("where do I go on Sunday? "). UsingTemplateFromFile ($" {Directory. GetCurrentDirectory}/EmailTemplage. cshtml ", new {Name =" Luke "})
Use Mailgun to send emails
Some people may not know about Mailgun. Mailgun is a foreign email service company. For example, the famous Github email service is hosted on it, the free Maingun account can send 10000 emails each month, which is sufficient for many small and medium websites.
When you use Mailgun to send emails, you need to register an account first, and then you can use the Rest API provided by Mailgun to manage sent or received emails. To use the Mailgun integrated by FluentEmail, you only need to add the following package:
Install-Package FluentEmail.Mailgun
After registering Mailgun, you will be assigned an API Key and a second-level domain name. In the program, you need to configure the following:
// Both.. NET Core and. NET Frameworkvar sender = new MailgunSender ("sandboxcf5f41bbf2f84f15a%c60e253b5fe8.mailgun.org", // Mailgun second-level domain name "key-8d32c046d7f14ada8d5ba8253e3e30df" // Mailgun API Key); Email. defaultSender = sender; var email = Email. from ("foo@email.com "). ("bar@email.com", "bob "). subject ("where do I go on Sunday? "). Body (" How do I go to the Palace Museum? "); Await email. SendAsync ();
Summary
Through the above example, we can see that MailKit and FluentEmail have their own advantages and disadvantages. MailKit supports many protocols and is cross-platform. However, it does not support Razor, and you need to integrate Mailgun yourself. The advantage of FlentEmail is that it provides support for Razor templates and encapsulates Mailgun. The disadvantage is that the SMTP protocol has not yet provided support for. NET Core.
To sum up, if you use Mailgun to send emails, you should choose FluentEmail. If you want to use the SMTP protocol to link to your email server to send emails, then you should use MailKit.
In ASP. the implementation method for sending emails in. NET Core (this article is required) is all the content shared by the editor. I hope to give you a reference, and I hope you can provide more support for the customer's house.